Skip to content

Commit

Permalink
refactor subject out of one spec
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed May 4, 2012
1 parent 1e4c1e6 commit 604dc5d
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions spec/rspec/mocks/serialization_spec.rb
Expand Up @@ -2,21 +2,22 @@

module RSpec
module Mocks
class SerializableStruct < Struct.new(:foo, :bar); end
describe Serialization do

class SerializableMockProxy
attr_reader :mock_proxy
class SerializableObject < Struct.new(:foo, :bar); end

def initialize(mock_proxy)
@mock_proxy = mock_proxy
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
def ==(other)
other.class == self.class && other.mock_proxy == mock_proxy
end
end
end

describe Serialization do
def self.with_yaml_loaded(&block)
context 'with YAML loaded' do
module_eval(&block)
Expand All @@ -25,7 +26,7 @@ def self.with_yaml_loaded(&block)

def self.without_yaml_loaded(&block)
context 'without YAML loaded' do
before(:each) do
before do
# We can't really unload yaml, but we can fake it here...
@orig_yaml_constant = Object.send(:remove_const, :YAML)
Struct.class_eval do
Expand All @@ -36,7 +37,7 @@ def self.without_yaml_loaded(&block)

module_eval(&block)

after(:each) do
after do
Object.const_set(:YAML, @orig_yaml_constant)
Struct.class_eval do
alias to_yaml __old_to_yaml
Expand All @@ -46,19 +47,19 @@ def self.without_yaml_loaded(&block)
end
end

subject { RSpec::Mocks::SerializableStruct.new(7, "something") }
let(:serializable_object) { RSpec::Mocks::SerializableObject.new(7, "something") }

def set_stub
subject.stub(:bazz => 5)
serializable_object.stub(:bazz => 5)
end

shared_examples_for 'normal YAML serialization' do
it 'serializes to yaml the same with and without stubbing, using #to_yaml' do
expect { set_stub }.to_not change { subject.to_yaml }
expect { set_stub }.to_not change { serializable_object.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) }
expect { set_stub }.to_not change { YAML.dump(serializable_object) }
end
end

Expand Down Expand Up @@ -87,22 +88,22 @@ def set_stub

without_yaml_loaded do
it 'does not add #to_yaml to the stubbed object' do
subject.should_not respond_to(:to_yaml)
serializable_object.should_not respond_to(:to_yaml)
set_stub
subject.should_not respond_to(:to_yaml)
serializable_object.should_not respond_to(:to_yaml)
end
end

it 'marshals the same with and without stubbing' do
expect { set_stub }.to_not change { Marshal.dump(subject) }
expect { set_stub }.to_not change { Marshal.dump(serializable_object) }
end

describe "an object that has its own mock_proxy instance variable" do
subject { RSpec::Mocks::SerializableMockProxy.new(:my_mock_proxy) }
let(:serializable_object) { RSpec::Mocks::SerializableMockProxy.new(:my_mock_proxy) }

it 'does not interfere with its marshalling' do
marshalled_copy = Marshal.load(Marshal.dump(subject))
marshalled_copy.should eq subject
marshalled_copy = Marshal.load(Marshal.dump(serializable_object))
marshalled_copy.should eq serializable_object
end
end
end
Expand Down

0 comments on commit 604dc5d

Please sign in to comment.