Permalink
Browse files

Clean up proxy specs with shared examples

  • Loading branch information...
joshuaclayton committed Aug 17, 2011
1 parent 0b2c4da commit d296a225864e50750b355741122f99177ed6a573
@@ -7,7 +7,6 @@ def initialize(klass)
end
def get(attribute)
- nil
end
def set(attribute, value)
@@ -63,7 +62,6 @@ def run_callbacks(name)
# FactoryGirl.create(:post)
#
def association(name, overrides = {})
- nil
end
def method_missing(method, *args, &block)
@@ -1,51 +1,42 @@
require 'spec_helper'
describe FactoryGirl::Proxy::AttributesFor do
- before do
- @proxy = FactoryGirl::Proxy::AttributesFor.new(@class)
- end
+ let(:proxy_class) { stub("class") }
- describe "when asked to associate with another factory" do
- before do
- FactoryGirl.stubs(:create)
- @proxy.associate(:owner, :user, {})
- end
+ subject { FactoryGirl::Proxy::AttributesFor.new(proxy_class) }
- it "should not set a value for the association" do
- (@proxy.result(nil).key?(:owner)).should_not be
- end
- end
+ it_should_behave_like "proxy without association support"
- it "should return nil when building an association" do
- @proxy.association(:user).should be_nil
+ it "returns a hash when asked for the result" do
+ subject.result(nil).should be_kind_of(Hash)
end
- it "should not call Factory.create when building an association" do
- FactoryGirl.stubs(:create)
- @proxy.association(:user).should be_nil
- FactoryGirl.should have_received(:create).never
- end
+ context "after associating a factory" do
+ let(:attribute) { :owner }
- it "should always return nil when building an association" do
- @proxy.set(:association, 'x')
- @proxy.association(:user).should be_nil
- end
+ before { subject.associate(attribute, :user, {}) }
- it "should return a hash when asked for the result" do
- @proxy.result(nil).should be_kind_of(Hash)
+ it "doesn't set that key in the resulting hash" do
+ subject.result(nil).should_not have_key(attribute)
+ end
+
+ it "returns nil when asked for that attribute" do
+ subject.get(attribute).should be_nil
+ end
end
describe "after setting an attribute" do
- before do
- @proxy.set(:attribute, 'value')
- end
+ let(:attribute) { :attribute }
+ let(:value) { "value" }
+
+ before { subject.set(attribute, value) }
- it "should set that value in the resulting hash" do
- @proxy.result(nil)[:attribute].should == 'value'
+ it "sets that value in the resulting hash" do
+ subject.result(nil)[attribute].should == value
end
- it "should return that value when asked for that attribute" do
- @proxy.get(:attribute).should == 'value'
+ it "returns that value when asked for that attribute" do
+ subject.get(attribute).should == value
end
end
end
@@ -1,77 +1,12 @@
require 'spec_helper'
describe FactoryGirl::Proxy::Build do
- before do
- @instance = stub("built-instance", :attribute => "value", :attribute= => nil, :owner= => nil)
- @class = stub("class", :new => @instance)
- @proxy = FactoryGirl::Proxy::Build.new(@class)
- end
+ let(:instance) { stub("built-instance") }
+ let(:proxy_class) { stub("class", :new => instance) }
- it "should instantiate the class" do
- @class.should have_received(:new)
- end
+ subject { FactoryGirl::Proxy::Build.new(proxy_class) }
- describe "when asked to associate with another factory" do
- before do
- @association = "associated-instance"
- @associated_factory = stub("associated-factory", :run => @association)
- FactoryGirl.stubs(:factory_by_name => @associated_factory)
- @overrides = { 'attr' => 'value' }
- @proxy.associate(:owner, :user, @overrides)
- end
-
- it "should create the associated instance" do
- @associated_factory.should have_received(:run).with(FactoryGirl::Proxy::Create, @overrides)
- end
-
- it "should set the associated instance" do
- @instance.should have_received(:owner=).with(@association)
- end
- end
-
- it "should run create when building an association" do
- association = "associated-instance"
- associated_factory = stub("associated-factory", :run => association)
- FactoryGirl.stubs(:factory_by_name => associated_factory)
- overrides = { 'attr' => 'value' }
- @proxy.association(:user, overrides).should == association
- associated_factory.should have_received(:run).with(FactoryGirl::Proxy::Create, overrides)
- end
-
- it "should return the built instance when asked for the result" do
- @proxy.result(nil).should == @instance
- end
-
- it "should run the :after_build callback when retrieving the result" do
- thing_to_call = stub("object", :foo => nil)
- @proxy.add_callback(:after_build, proc{ thing_to_call.foo })
- @proxy.result(nil)
- thing_to_call.should have_received(:foo)
- end
-
- describe "when setting an attribute" do
- before do
- @instance.stubs(:attributes=)
- @proxy.set(:attribute, 'value')
- end
-
- it "should set that value" do
- @instance.should have_received(:attribute=).with('value')
- end
- end
-
- describe "when getting an attribute" do
- before do
- @result = @proxy.get(:attribute)
- end
-
- it "should ask the built class for the value" do
- @instance.should have_received(:attribute)
- end
-
- it "should return the value for that attribute" do
- @result.should == 'value'
- end
- end
+ it_should_behave_like "proxy with association support", FactoryGirl::Proxy::Create
+ it_should_behave_like "proxy with standard getters and setters", :attribute_name, "attribute value!"
+ it_should_behave_like "proxy with callbacks", :after_build
end
-
@@ -1,95 +1,42 @@
require 'spec_helper'
describe FactoryGirl::Proxy::Create do
- before do
- @instance = stub("built-instance", :attribute => "value", :attribute= => nil, :owner= => nil, :save! => nil)
- @class = stub("class", :new => @instance)
- @proxy = FactoryGirl::Proxy::Create.new(@class)
- end
-
- it "should instantiate the class" do
- @class.should have_received(:new)
- end
- describe "when asked to associate with another factory" do
- before do
- @association = "associated-instance"
- @associated_factory = stub("associated-factory", :run => @association)
- FactoryGirl.stubs(:factory_by_name => @associated_factory)
- @overrides = { 'attr' => 'value' }
- @proxy.associate(:owner, :user, @overrides)
- end
+ let(:instance) { stub("created-instance", :save! => true) }
+ let(:proxy_class) { stub("class", :new => instance) }
- it "should create the associated instance" do
- @associated_factory.should have_received(:run).with(FactoryGirl::Proxy::Create, @overrides)
- end
+ subject { FactoryGirl::Proxy::Create.new(proxy_class) }
- it "should set the associated instance" do
- @instance.should have_received(:owner=).with(@association)
- end
- end
+ it_should_behave_like "proxy with association support", FactoryGirl::Proxy::Create
+ it_should_behave_like "proxy with standard getters and setters", :attribute_name, "attribute value!"
+ it_should_behave_like "proxy with callbacks", :after_build
+ it_should_behave_like "proxy with callbacks", :after_create
- it "should run create when building an association" do
- association = "associated-instance"
- associated_factory = stub("associated-factory", :run => association)
- FactoryGirl.stubs(:factory_by_name => associated_factory)
- overrides = { 'attr' => 'value' }
- @proxy.association(:user, overrides).should == association
- associated_factory.should have_received(:run).with(FactoryGirl::Proxy::Create, overrides)
- end
-
- describe "when asked for the result" do
- before do
- @build_spy = stub("build", :foo => nil)
- @create_spy = stub("create", :foo => nil)
- @proxy.add_callback(:after_build, proc{ @build_spy.foo })
- @proxy.add_callback(:after_create, proc{ @create_spy.foo })
- @result = @proxy.result(nil)
- end
-
- it "should save the instance" do
- @instance.should have_received(:save!)
- end
-
- it "should return the built instance" do
- @result.should == @instance
- end
-
- it "should run both the build and the create callbacks" do
- @build_spy.should have_received(:foo)
- @create_spy.should have_received(:foo)
- end
+ it "saves the instance before returning the result" do
+ subject.result(nil)
+ instance.should have_received(:save!)
end
it "runs a custom create block" do
block = stub('custom create block', :call => nil)
- @instance.stubs(:save!).raises(RuntimeError)
- instance = @proxy.result(block)
+ subject.result(block)
block.should have_received(:call).with(instance)
+ instance.should have_received(:save!).never
end
- describe "when setting an attribute" do
- before do
- @proxy.set(:attribute, 'value')
- end
+ context "callback execution order" do
+ let(:after_build_callback) { stub("after_build callback", :foo => nil) }
+ let(:after_create_callback) { stub("after_create callback", :foo => nil) }
+ let(:callback_sequence) { sequence("after_* callbacks") }
- it "should set that value" do
- @instance.should have_received(:attribute=).with('value')
- end
- end
+ it "runs after_build callbacks before after_create callbacks" do
+ subject.add_callback(:after_build, proc { after_build_callback.foo })
+ subject.add_callback(:after_create, proc { after_create_callback.foo })
- describe "when getting an attribute" do
- before do
- @result = @proxy.get(:attribute)
- end
+ after_build_callback.expects(:foo).once.in_sequence(callback_sequence)
+ after_create_callback.expects(:foo).once.in_sequence(callback_sequence)
- it "should ask the built class for the value" do
- @instance.should have_received(:attribute)
- end
-
- it "should return the value for that attribute" do
- @result.should == 'value'
+ subject.result(nil)
end
end
end
-
Oops, something went wrong.

0 comments on commit d296a22

Please sign in to comment.