Skip to content

Commit

Permalink
Issue #120: Under any_instance, modify #dup to remove the mock proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiwren committed May 20, 2012
1 parent 7d85604 commit 5de6500
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
35 changes: 34 additions & 1 deletion lib/rspec/mocks/any_instance.rb
Expand Up @@ -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

Expand All @@ -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
4 changes: 4 additions & 0 deletions lib/rspec/mocks/methods.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/rspec/mocks/any_instance/issue_120.rb
Expand Up @@ -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
Expand Down

0 comments on commit 5de6500

Please sign in to comment.