Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix marshal extension #29

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/rspec/mocks/extensions/marshal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions spec/rspec/mocks/serialization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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