Commit
[git-p4: depot-paths = "//src/minitest/dev/": change = 7432]
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,3 +125,41 @@ def respond_to?(sym) # :nodoc: | |
end | ||
end | ||
end | ||
|
||
class Object # :nodoc: | ||
|
||
## | ||
# 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. Cleans up the stub at the end of the +block+. | ||
# | ||
# 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 | ||
|
||
def stub name, val_or_callable, &block | ||
new_name = "__minitest_stub__#{name}" | ||
|
||
metaclass = class << self; self; end | ||
metaclass.send :alias_method, new_name, name | ||
metaclass.send :define_method, name do |*args| | ||
if val_or_callable.respond_to? :call then | ||
val_or_callable.call(*args) | ||
else | ||
val_or_callable | ||
end | ||
end | ||
|
||
yield | ||
ensure | ||
metaclass.send :undef_method, name | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
phiggins
|
||
metaclass.send :alias_method, name, new_name | ||
metaclass.send :undef_method, new_name | ||
end | ||
end |
Hi @zenspider, I was reviewing how Minitest implements stubs. I just want to know, why it undefs the method if then will be overwritten by the alias? Maybe I'm missing something. Anyway, I would be very grateful if you could answer this. thanks!