No release in over 3 years
Low commit activity in last 3 years
Stub constants for the duration of a block in MiniTest
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

minitest-stub-const

Build Status Gem

Stub constants for the duration of a block in MiniTest.
Similar to RSpec's stub_const.

Example

Stub a constant for the duration of a block:

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:

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

gem install minitest-stub-const

Or add gem "minitest-stub-const" to your Gemfile and run bundle install.

Then require "minitest/stub_const" on your test_helper.rb or test file.

License

minitest-stub-const is free software, available under the MIT license.