class Object
Public Instance Methods
silence_warnings() { || ... }
click to toggle source
# File lib/minitest/stub_const.rb, line 60 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 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
# File lib/minitest/stub_const.rb, line 19 def stub_const(name, val, &block) defined = const_defined?(name) orig = const_get(name) if defined silence_warnings { const_set(name, val) } yield ensure if defined silence_warnings { const_set(name, orig) } else remove_const(name) end end
stub_remove_const(name) { || ... }
click to toggle source
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!"
# File lib/minitest/stub_const.rb, line 43 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