Skip to content

Commit

Permalink
Merge pull request #234 from JonRowe/fix_issue_with_stubbing_any_inst…
Browse files Browse the repository at this point in the history
…ance_and_dup

Fix issue #229 runtime error caused by duplicating an object which is frozen and stubbing with any_instance
  • Loading branch information
myronmarston committed Mar 4, 2013
2 parents 73d7236 + e0eca44 commit 12532f8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/rspec/mocks/any_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ def modify_dup_to_remove_mock_proxy_when_invoked
if method_defined?(:dup) and !method_defined?(:__rspec_original_dup)
class_eval do
def __rspec_dup(*arguments, &block)
__remove_mock_proxy
__rspec_original_dup(*arguments, &block)
clone = __rspec_original_dup(*arguments, &block)
clone.send :__remove_mock_proxy
clone
end

alias_method :__rspec_original_dup, :dup
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/mocks/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def __mock_proxy
mp
end
end

def __remove_mock_proxy
@mock_proxy = nil
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/mocks/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def expectation_ordering
end

private

def receivers
@receivers ||= []
end
Expand Down
7 changes: 7 additions & 0 deletions spec/rspec/mocks/any_instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ def private_method; :private_method_return_value; end
klass.any_instance.stub(:foo).and_return(result = Object.new)
expect(instance.foo).to eq(result)
end

it 'handles freeze and duplication correctly' do
String.any_instance.stub(:any_method)

foo = 'foo'.freeze
expect(foo.dup.concat 'bar').to eq 'foobar'
end
end

context "with argument matching" do
Expand Down

0 comments on commit 12532f8

Please sign in to comment.