minitest-stub-const-0.4/0000755000076400007640000000000012511732131014340 5ustar pravipraviminitest-stub-const-0.4/LICENSE0000644000076400007640000000205412511732131015346 0ustar pravipraviCopyright (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.4/README.md0000644000076400007640000000154712511732131015626 0ustar pravipravi# 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. Like RSpec's [stub_const] [rspec], but boring and non-magical. ## Example ```ruby it "calls a Thing.add when add_thing is called" do m = MiniTest::Mock.new m.expect(:add, nil) MyLib.stub_const(:Thing, m) do @subject.add_thing end m.verify 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.4/test/0000755000076400007640000000000012511732131015317 5ustar pravipraviminitest-stub-const-0.4/test/test_stub_const.rb0000644000076400007640000000212412511732131021065 0ustar pravipravirequire File.expand_path('../../lib/minitest/stub_const', __FILE__) require 'minitest/mock' module A module B def self.what :old 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 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 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 end end minitest-stub-const-0.4/metadata.yml0000644000076400007640000000206112511732131016642 0ustar pravipravi--- !ruby/object:Gem::Specification name: minitest-stub-const version: !ruby/object:Gem::Version version: '0.4' platform: ruby authors: - Adam Mckaig autorequire: bindir: bin cert_chain: [] date: 2015-03-23 00:00:00.000000000 Z dependencies: [] description: email: - adam.mckaig@gmail.com executables: [] extensions: [] extra_rdoc_files: [] files: - lib/minitest/stub/const.rb - lib/minitest/stub_const.rb - LICENSE - README.md - test/test_stub_const.rb homepage: https://github.com/adammck/minitest-stub-const licenses: - MIT metadata: {} post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: rubygems_version: 2.0.3 signing_key: specification_version: 4 summary: Stub constants for the duration of a block in MiniTest test_files: - test/test_stub_const.rb minitest-stub-const-0.4/lib/0000755000076400007640000000000012511732131015106 5ustar pravipraviminitest-stub-const-0.4/lib/minitest/0000755000076400007640000000000012511732131016742 5ustar pravipraviminitest-stub-const-0.4/lib/minitest/stub_const.rb0000644000076400007640000000254212511732131021455 0ustar pravipraviclass Object # # Replace the +value+ of constant +name+ for the duration of a +block+. This # is useful when testing that the expected class methods are being called on # a Module or Class instance. # # Example: # # m = MiniTest::Mock.new # m.expect(:register, nil, [:whatever]) # # MyLib.stub_const(:Thing, m) do # @subject.add_thing(:whatever) # end # # m.verify # def stub_const(name, val, &block) orig = const_get(name) silence_warnings do const_set(name, val) end yield ensure silence_warnings do const_set(name, orig) 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. # # Example: # # Object.stub_remove_const(:File) do # "Look ma, no File!" unless defined(File) # end # # => "Look ma, no File!" def stub_remove_const(name) orig = const_get(name) remove_const(name) yield ensure const_set(name, orig) 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.4/lib/minitest/stub/0000755000076400007640000000000012511732131017717 5ustar pravipraviminitest-stub-const-0.4/lib/minitest/stub/const.rb0000644000076400007640000000006712511732131021375 0ustar pravipravirequire File.expand_path("../../stub_const", __FILE__)