Class: Object
Do not use. This class is for internal use only.
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
::Minitest::Expectations
|
|
Inherits: | BasicObject |
Defined in: | lib/minitest/mock.rb, lib/minitest/spec.rb |
Overview
Object
extensions for ::Minitest::Mock
.
Instance Method Summary
-
#stub(name, val_or_callable, *block_args)
Add a temporary stubbed method replacing
name
for the duration of theblock
.
::Minitest::Expectations
- Included
Instance Method Details
#stub(name, val_or_callable, *block_args)
Add a temporary stubbed method replacing name
for the duration of the block
. If val_or_callable
responds to #call
, then it returns the result of calling it, otherwise returns the value as-is. If stubbed method yields a block, block_args
will be passed along. Cleans up the stub at the end of the block
. The method name
must exist before stubbing.
def test_stale_eh
obj_under_test = Something.new
refute obj_under_test.stale?
Time.stub :now, Time.at(0) do
assert obj_under_test.stale?
end
end
# File 'lib/minitest/mock.rb', line 214
def stub name, val_or_callable, *block_args new_name = "__minitest_stub__#{name}" = class << self; self; end if respond_to? name and not methods.map(&:to_s).include? name.to_s then .send :define_method, name do |*args| super(*args) end end .send :alias_method, new_name, name .send :define_method, name do |*args, &blk| if val_or_callable.respond_to? :call then val_or_callable.call(*args, &blk) else blk.call(*block_args) if blk val_or_callable end end .send(:ruby2_keywords, name) if .respond_to?(:ruby2_keywords, true) yield self ensure .send :undef_method, name .send :alias_method, name, new_name .send :undef_method, new_name end