Permalink
Browse files

Issue #120: Under any_instance, modify #dup to remove the mock proxy

  • Loading branch information...
1 parent 7d85604 commit 5de650080a9de6723d997a142537b6a05be8c115 @kaiwren kaiwren committed May 20, 2012
Showing with 39 additions and 2 deletions.
  1. +34 −1 lib/rspec/mocks/any_instance.rb
  2. +4 −0 lib/rspec/mocks/methods.rb
  3. +1 −1 spec/rspec/mocks/any_instance/issue_120.rb
@@ -24,6 +24,7 @@ module AnyInstance
# @return [Recorder]
def any_instance
RSpec::Mocks::space.add(self)
+ modify_dup_to_remove_mock_proxy_when_invoked
__recorder
end
@@ -33,13 +34,45 @@ def rspec_verify
super
ensure
__recorder.stop_all_observation!
+ restore_dup
@__recorder = nil
end
-
+
+ # @private
+ def rspec_reset
+ restore_dup
+ __mock_proxy.reset
+ end
+
# @private
def __recorder
@__recorder ||= AnyInstance::Recorder.new(self)
end
+
+ private
+ def modify_dup_to_remove_mock_proxy_when_invoked
+ unless self.method_defined?(:__rspec_original_dup)
+ self.class_eval do
+ def __rspec_dup
+ __remove_mock_proxy
+ __rspec_original_dup
+ end
+
+ alias_method :__rspec_original_dup, :dup
+ alias_method :dup, :__rspec_dup
+ end
+ end
+ end
+
+ def restore_dup
+ if self.method_defined?(:__rspec_original_dup)
+ self.class_eval do
+ alias_method :dup, :__rspec_original_dup
+ remove_method :__rspec_original_dup
+ remove_method :__rspec_dup
+ end
+ end
+ end
end
end
end
@@ -135,6 +135,10 @@ def __mock_proxy
mp
end
end
+
+ def __remove_mock_proxy
+ @mock_proxy = nil
+ end
def format_chain(*chain, &blk)
if Hash === chain.last
@@ -7,7 +7,7 @@ module Mocks
Object.any_instance.stub(:some_method)
o = Object.new
o.some_method
- o.dup.some_method
+ lambda { o.dup.some_method }.should_not raise_error(SystemStackError)
end
end
end

0 comments on commit 5de6500

Please sign in to comment.