diff --git a/lib/factory_girl/definition.rb b/lib/factory_girl/definition.rb index 210eadd96..f5cb42343 100644 --- a/lib/factory_girl/definition.rb +++ b/lib/factory_girl/definition.rb @@ -6,7 +6,7 @@ def initialize(name = nil) @declarations = DeclarationList.new(name) @callbacks = [] @defined_traits = [] - @to_create = nil + @to_create = lambda {|instance| instance.save! } @traits = [] end diff --git a/lib/factory_girl/proxy/create.rb b/lib/factory_girl/proxy/create.rb index c92f3df09..101e02815 100644 --- a/lib/factory_girl/proxy/create.rb +++ b/lib/factory_girl/proxy/create.rb @@ -4,7 +4,6 @@ class Create < Build #:nodoc: def result(to_create) super - to_create ||= lambda {|instance| instance.save! } to_create[@instance] run_callbacks(:after_create) diff --git a/spec/factory_girl/definition_spec.rb b/spec/factory_girl/definition_spec.rb index 4ea0079a2..e70794970 100644 --- a/spec/factory_girl/definition_spec.rb +++ b/spec/factory_girl/definition_spec.rb @@ -49,7 +49,13 @@ end describe FactoryGirl::Definition, "#to_create" do - its(:to_create) { should be_nil } + its(:to_create) { should be_a(Proc) } + + it "calls save! on the object when run" do + instance = stub("model instance", :save! => true) + subject.to_create[instance] + instance.should have_received(:save!).once + end it "returns the assigned value when given a block" do block = proc { nil } diff --git a/spec/factory_girl/factory_spec.rb b/spec/factory_girl/factory_spec.rb index 874e4c880..9ebecd9c1 100644 --- a/spec/factory_girl/factory_spec.rb +++ b/spec/factory_girl/factory_spec.rb @@ -271,7 +271,7 @@ it "returns the result from the proxy when running" do subject.run(FactoryGirl::Proxy::Build, {}).should == "result" - proxy.should have_received(:result).with(nil) + proxy.should have_received(:result).with(subject.definition.to_create) end it "sets overrides once on the factory" do diff --git a/spec/factory_girl/proxy/create_spec.rb b/spec/factory_girl/proxy/create_spec.rb index 75fa2625d..fc107a081 100644 --- a/spec/factory_girl/proxy/create_spec.rb +++ b/spec/factory_girl/proxy/create_spec.rb @@ -11,11 +11,6 @@ it_should_behave_like "proxy with callbacks", :after_build it_should_behave_like "proxy with callbacks", :after_create - 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_run = false block = lambda {|instance| block_run = true } @@ -23,7 +18,6 @@ instance.should have_received(:save!).never block_run.should be_true end - end describe FactoryGirl::Proxy::Create, "when running callbacks" do @@ -38,7 +32,7 @@ subject { FactoryGirl::Proxy::Create.new(proxy_class, [after_create_one, after_create_two, after_build_one]) } it "runs callbacks in the correct order" do - subject.result(nil) + subject.result(lambda {|instance| instance }) callback_result.should == [:after_build_one, :after_create_one, :after_create_two] end end diff --git a/spec/support/shared_examples/proxy.rb b/spec/support/shared_examples/proxy.rb index 144010894..f59e808f2 100644 --- a/spec/support/shared_examples/proxy.rb +++ b/spec/support/shared_examples/proxy.rb @@ -102,11 +102,11 @@ subject { described_class.new(proxy_class, [callback]) } it "runs the #{callback_name} callback" do - subject.result(nil) + subject.result(lambda {|instance| instance }) callback_instance.should have_received(:foo).once end it "returns the proxy instance" do - subject.result(nil).should == instance + subject.result(lambda {|instance| instance }).should == instance end end