Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move logic of what to do to create an instance to the definition wher…

…e it belongs
  • Loading branch information...
commit 5bbbcb9edfb82f83f88d57f712d123c0ce85e348 1 parent e15ae8d
@joshuaclayton joshuaclayton authored
View
2  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
View
1  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)
View
8 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 }
View
2  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
View
8 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
View
4 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
Please sign in to comment.
Something went wrong with that request. Please try again.