Class: Object

Inherits:
BasicObject
Defined in:
lib/minitest/stub_const.rb

Instance Method Summary collapse

Instance Method Details

#silence_warningsObject



36
37
38
39
40
41
42
# File 'lib/minitest/stub_const.rb', line 36

def silence_warnings
  orig = $VERBOSE
  $VERBOSE = nil
  yield
ensure
  $VERBOSE = orig
end

#stub_const(name, val, &block) ⇒ 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


19
20
21
22
23
24
25
26
27
28
29
30
31
# 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