Skip to content

Commit

Permalink
Merge pull request rspec#138 from c42engineering/issue_120
Browse files Browse the repository at this point in the history
Issue rspec#120
  • Loading branch information
justinko committed May 23, 2012
2 parents a11fcdc + 5de6500 commit bab88c7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
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
14 changes: 14 additions & 0 deletions spec/rspec/mocks/any_instance/issue_120.rb
@@ -0,0 +1,14 @@
require 'spec_helper'

module RSpec
module Mocks
describe "#any_instance" do
it "doesn't cause an infinite loop when used in conjunction with dup" do
Object.any_instance.stub(:some_method)
o = Object.new
o.some_method
lambda { o.dup.some_method }.should_not raise_error(SystemStackError)
end
end
end
end

0 comments on commit bab88c7

Please sign in to comment.