minitest-stub-const-0.6/0000755000004100000410000000000013411314735015344 5ustar www-datawww-dataminitest-stub-const-0.6/test/0000755000004100000410000000000013411314735016323 5ustar www-datawww-dataminitest-stub-const-0.6/test/test_stub_const.rb0000644000004100000410000000463613411314735022103 0ustar www-datawww-datarequire File.expand_path('../../lib/minitest/stub_const', __FILE__) require 'minitest/autorun' require 'minitest/mock' module A module B def self.what :old end end module C def self.who :you end end end describe 'Object' do describe '#stub_const' do before do @mock = MiniTest::Mock.new @mock.expect(:what, :new) end it 'replaces a constant for the duration of a block' do A.stub_const(:B, @mock) do assert_equal :new, A::B.what end @mock.verify end it 'restores the original value after the block' do A.stub_const(:B, @mock) { } assert_equal :old, A::B.what end it 'does not raise any warnings' do assert_silent { A.stub_const(:B, @mock) { } } end it 'should stub undefined constants' do refute defined?(A::X) A.stub_const(:X, @mock) do assert_equal :new, A::X.what end refute defined?(A::X) @mock.verify end end describe '#stub_remove_const' do it 'removes a constant for the duration of a block' do A.stub_remove_const(:B) do refute defined?(A::B) end end it 'restores the original value after the block' do A.stub_remove_const(:B) { } assert_equal :old, A::B.what end it 'does not raise any warnings' do assert_silent { A.stub_remove_const(:B) { } } end it 'leaves undefined constants undefined' do refute defined?(A::X) A.stub_remove_const(:X) { } refute defined?(A::X) end end describe '#stub_consts' do before do @mock = MiniTest::Mock.new @mock.expect(:what, :new) @mock2 = MiniTest::Mock.new @mock2.expect(:who, :me) @mock3 = MiniTest::Mock.new @mock3.expect(:where, :there) end def run_stub_block A.stub_consts(B: @mock, C: @mock2, D: @mock3) do assert_equal :new, A::B.what assert_equal :me, A::C.who assert_equal :there, A::D.where end end it 'replaces a set of constants for the duration of a block' do run_stub_block @mock.verify @mock2.verify @mock3.verify end it 'restores previous values' do run_stub_block assert_equal :old, A::B.what assert_equal :you, A::C.who end it 'removes constants that were undefined prior to the block' do run_stub_block refute A.const_defined?(:D) end end end minitest-stub-const-0.6/README.md0000644000004100000410000000262713411314735016632 0ustar www-datawww-data# minitest-stub-const [![Build Status](https://travis-ci.org/adammck/minitest-stub-const.svg)](https://travis-ci.org/adammck/minitest-stub-const) Stub constants for the duration of a block in MiniTest. Similar to RSpec's [stub_const] [rspec]. ## Example Stub a constant for the duration of a block: ```ruby module Foo BAR = :original end Foo.stub_const(:BAR, :stubbed) do Foo::BAR end # => :stubbed Foo::BAR # => :original ``` This is especially useful when testing that the expected class methods are being called on a `Module` or `Class` instance: ```ruby module SomeLib class Thing def self.add fail NotImplementedError end end end class ThingAdder def add_thing SomeLib::Thing.add end end describe ThingAdder do describe '#add_thing' do it 'should call Thing.add' do adder = ThingAdder.new mock = Minitest::Mock.new mock.expect(:add, nil) SomeLib.stub_const(:Thing, mock) do adder.add_thing end assert mock.verify end end end ``` ## Installation ```sh gem install minitest-stub-const # duh ``` ## License [minitest-stub-const] [repo] is free software, available under [the MIT license] [license]. [repo]: https://raw.github.com/adammck/minitest-stub-const [license]: https://raw.github.com/adammck/minitest-stub-const/master/LICENSE [rspec]: https://www.relishapp.com/rspec/rspec-mocks/v/2-12/docs/mutating-constants minitest-stub-const-0.6/LICENSE0000644000004100000410000000205413411314735016352 0ustar www-datawww-dataCopyright (c) 2012 Adam Mckaig MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. minitest-stub-const-0.6/lib/0000755000004100000410000000000013411314735016112 5ustar www-datawww-dataminitest-stub-const-0.6/lib/minitest/0000755000004100000410000000000013411314735017746 5ustar www-datawww-dataminitest-stub-const-0.6/lib/minitest/stub_const.rb0000644000004100000410000000451313411314735022461 0ustar www-datawww-dataclass Object # Replace the +value+ of constant +name+ for the duration of a # +block+. This is especially useful when testing that the expected # class methods are being called on a Module or Class instance. # # Example: # # module Foo # BAR = :original # end # # Foo.stub_const(:BAR, :stubbed) do # Foo::BAR # end # # => :stubbed # # Foo::BAR # # => :original def stub_const(name, val = nil, &block) stub_consts(name => val, &block) end # Same as stub_const except it supports a Hash of +name+ to +value+ pairs # of the constants that will be stubbed for the duration of the +block+. # # Example: # # module Foo # BAR = :original1 # BAZ = :original2 # end # # Foo.stub_consts(BAR: :stubble, BAZ: :stubby) do # [Foo::BAR, Foo::BAZ] # end # # => [:stubble, :stubby] # # [Foo::BAR, Foo::BAZ] # # => [:original1, :original2] def stub_consts(consts, &block) original_values = {} consts.each_pair do |name, val| if const_defined?(name) original_value = const_get(name) original_values[name] = original_value end silence_warnings { const_set(name, val) } end yield ensure consts.keys.each do |name| if original_values.key?(name) silence_warnings { const_set(name, original_values[name]) } else remove_const(name) end end end # Remove the constant +name+ for the duration of a block. This is # useful when testing code that checks whether a constant is defined # or not. Simply yields to the passed block if the constant is not # currently defined. # # Example: # # Object.stub_remove_const(:File) do # "Look ma, no File!" unless defined?(File) # end # # => "Look ma, no File!" def stub_remove_const(name) if const_defined?(name) begin orig = const_get(name) remove_const(name) yield ensure const_set(name, orig) end else yield end end # Add a minimal implementation of ActiveSupport's silence_warnings if it # hasn't already been defined, to call a block with warnings disabled. unless respond_to?(:silence_warnings) def silence_warnings orig = $VERBOSE $VERBOSE = nil yield ensure $VERBOSE = orig end end end minitest-stub-const-0.6/lib/minitest/stub/0000755000004100000410000000000013411314735020723 5ustar www-datawww-dataminitest-stub-const-0.6/lib/minitest/stub/const.rb0000644000004100000410000000006713411314735022401 0ustar www-datawww-datarequire File.expand_path("../../stub_const", __FILE__) minitest-stub-const-0.6/minitest-stub-const.gemspec0000644000004100000410000000177513411314735022656 0ustar www-datawww-data######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: minitest-stub-const 0.6 ruby lib Gem::Specification.new do |s| s.name = "minitest-stub-const".freeze s.version = "0.6" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["Adam Mckaig".freeze] s.date = "2016-11-29" s.email = ["adam.mckaig@gmail.com".freeze] s.files = ["LICENSE".freeze, "README.md".freeze, "lib/minitest/stub/const.rb".freeze, "lib/minitest/stub_const.rb".freeze, "test/test_stub_const.rb".freeze] s.homepage = "https://github.com/adammck/minitest-stub-const".freeze s.licenses = ["MIT".freeze] s.rubygems_version = "2.5.2.1".freeze s.summary = "Stub constants for the duration of a block in MiniTest".freeze s.test_files = ["test/test_stub_const.rb".freeze] end