Permalink
Browse files

Fix our Marshal extension so that it does not interfere with objects …

…that have their own @mock_proxy instance variable.

Since our Marshal extension is loaded globally and affects every Marshal.dump call, we need to be extra careful not to unintentionally affect other things that rely on it.

- Closes #29.
  • Loading branch information...
1 parent 80dbde5 commit 6da62dc4aa0a71d2d02b1a71a5c49fe683f3679a @myronmarston myronmarston committed with dchelimsky Nov 7, 2010
Showing with 23 additions and 0 deletions.
  1. +2 −0 lib/rspec/mocks/extensions/marshal.rb
  2. +21 −0 spec/rspec/mocks/serialization_spec.rb
@@ -7,6 +7,8 @@ def dump_with_mocks(*args)
return dump_without_mocks(*args.unshift(object)) unless object.instance_variable_defined?(:@mock_proxy)
mp = object.instance_variable_get(:@mock_proxy)
+ return dump_without_mocks(*args.unshift(object)) unless mp.is_a?(::RSpec::Mocks::Proxy)
+
object.send(:remove_instance_variable, :@mock_proxy)
begin
@@ -5,6 +5,18 @@ module RSpec
module Mocks
class SerializableStruct < Struct.new(:foo, :bar); end
+ class SerializableMockProxy
+ attr_reader :mock_proxy
+
+ def initialize(mock_proxy)
+ @mock_proxy = mock_proxy
+ end
+
+ def ==(other)
+ other.class == self.class && other.mock_proxy == mock_proxy
+ end
+ end
+
describe Serialization do
def self.with_yaml_loaded(&block)
context 'with YAML loaded' do
@@ -62,6 +74,15 @@ def set_stub
it 'marshals the same with and without stubbing' do
expect { set_stub }.to_not change { Marshal.dump(subject) }
end
+
+ describe "an object that has its own mock_proxy instance variable" do
+ subject { SerializableMockProxy.new(:my_mock_proxy) }
+
+ it 'does not interfere with its marshalling' do
+ marshalled_copy = Marshal.load(Marshal.dump(subject))
+ marshalled_copy.should == subject
+ end
+ end
end
end
end

0 comments on commit 6da62dc

Please sign in to comment.