diff options
author | ryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-05-09 04:09:25 +0000 |
---|---|---|
committer | ryan <ryan@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-05-09 04:09:25 +0000 |
commit | a213c2ed9c84f0f64f0664e820227034d078f9f9 (patch) | |
tree | dbc503f2118fdb51f0c45ebf36ead1b799286468 /lib/minitest/mock.rb | |
parent | 2dece928e085d25e8326455b9c0f508a2d70d02d (diff) |
Imported minitest 3.0.0 (r7435) w/ fixes for rubygems. 10955 tests, 2253343 assertions, 1 failures, 1 errors, 28 skips minus drb tests on x86_64-darwin11.3.0 and reviewed by drbrain
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35601 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/minitest/mock.rb')
-rw-r--r-- | lib/minitest/mock.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/minitest/mock.rb b/lib/minitest/mock.rb index 55b0095c0b..ec36d775cc 100644 --- a/lib/minitest/mock.rb +++ b/lib/minitest/mock.rb @@ -132,3 +132,41 @@ module MiniTest 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 + metaclass.send :alias_method, name, new_name + metaclass.send :undef_method, new_name + end +end |