Permalink
Browse files

Fixed YAML serialization of a stubbed object.

Previously, the mock proxy would be serialized when #to_yaml is called on a stubbed object.  The mock proxy includes a reference to a proc.  When the yaml string is deserialized with YAML.load, you'd get an "allocator undefined for Proc" TypeError.

This fix prevents stubbing from setting any new instance variables on the stubbed object, so that the YAMl serialization is not affected.
  • Loading branch information...
1 parent 7499777 commit 793bd343ad1d0242e049fd7a3187c0f593498944 @myronmarston myronmarston committed with dchelimsky Sep 26, 2010
Showing with 35 additions and 5 deletions.
  1. +11 −5 lib/rspec/mocks/methods.rb
  2. +24 −0 spec/rspec/mocks/serialization_spec.rb
@@ -72,14 +72,20 @@ def null_object?
private
- def __mock_proxy
- if Mock === self
- @mock_proxy ||= Proxy.new(self, @name, @options)
- else
- @mock_proxy ||= Proxy.new(self)
+ @@mock_proxies = Hash.new do |hash, key|
+ hash[key] = key.instance_eval do
+ if Mock === self
+ Proxy.new(self, @name, @options)
+ else
+ Proxy.new(self)
+ end
end
end
+ def __mock_proxy
+ @@mock_proxies[self]
+ end
+
def format_chain(*chain, &blk)
if Hash === chain.last
hash = chain.pop
@@ -0,0 +1,24 @@
+require 'spec_helper'
+require 'yaml'
+
+module RSpec
+ module Mocks
+ class SerializableStruct < Struct.new(:foo, :bar); end
+
+ describe "Serialization" do
+ subject { SerializableStruct.new(7, "something") }
+
+ def set_stub
+ subject.stub(:bazz => 5)
+ end
+
+ it 'serializes to yaml the same with and without stubbing, using #to_yaml' do
+ expect { set_stub }.to_not change { subject.to_yaml }
+ end
+
+ it 'serializes to yaml the same with and without stubbing, using YAML.dump' do
+ expect { set_stub }.to_not change { YAML.dump(subject) }
+ end
+ end
+ end
+end

0 comments on commit 793bd34

Please sign in to comment.