class Object

Public Instance Methods

silence_warnings() { || ... } click to toggle source
# File lib/minitest/stub_const.rb, line 36
def silence_warnings
  orig = $VERBOSE
  $VERBOSE = nil
  yield
ensure
  $VERBOSE = orig
end
stub_const(name, val) { || ... } click to toggle source

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
# File lib/minitest/stub_const.rb, line 19
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