Skip to content
This repository
Browse code

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
Joshua Clayton authored November 25, 2011
2  lib/factory_girl/definition.rb
@@ -6,7 +6,7 @@ def initialize(name = nil)
6 6
       @declarations   = DeclarationList.new(name)
7 7
       @callbacks      = []
8 8
       @defined_traits = []
9  
-      @to_create      = nil
  9
+      @to_create      = lambda {|instance| instance.save! }
10 10
       @traits         = []
11 11
     end
12 12
 
1  lib/factory_girl/proxy/create.rb
@@ -4,7 +4,6 @@ class Create < Build #:nodoc:
4 4
       def result(to_create)
5 5
         super
6 6
 
7  
-        to_create ||= lambda {|instance| instance.save! }
8 7
         to_create[@instance]
9 8
 
10 9
         run_callbacks(:after_create)
8  spec/factory_girl/definition_spec.rb
@@ -49,7 +49,13 @@
49 49
 end
50 50
 
51 51
 describe FactoryGirl::Definition, "#to_create" do
52  
-  its(:to_create) { should be_nil }
  52
+  its(:to_create) { should be_a(Proc) }
  53
+
  54
+  it "calls save! on the object when run" do
  55
+    instance = stub("model instance", :save! => true)
  56
+    subject.to_create[instance]
  57
+    instance.should have_received(:save!).once
  58
+  end
53 59
 
54 60
   it "returns the assigned value when given a block" do
55 61
     block = proc { nil }
2  spec/factory_girl/factory_spec.rb
@@ -271,7 +271,7 @@
271 271
 
272 272
   it "returns the result from the proxy when running" do
273 273
     subject.run(FactoryGirl::Proxy::Build, {}).should == "result"
274  
-    proxy.should have_received(:result).with(nil)
  274
+    proxy.should have_received(:result).with(subject.definition.to_create)
275 275
   end
276 276
 
277 277
   it "sets overrides once on the factory" do
8  spec/factory_girl/proxy/create_spec.rb
@@ -11,11 +11,6 @@
11 11
   it_should_behave_like "proxy with callbacks", :after_build
12 12
   it_should_behave_like "proxy with callbacks", :after_create
13 13
 
14  
-  it "saves the instance before returning the result" do
15  
-    subject.result(nil)
16  
-    instance.should have_received(:save!)
17  
-  end
18  
-
19 14
   it "runs a custom create block" do
20 15
     block_run = false
21 16
     block = lambda {|instance| block_run = true }
@@ -23,7 +18,6 @@
23 18
     instance.should have_received(:save!).never
24 19
     block_run.should be_true
25 20
   end
26  
-
27 21
 end
28 22
 
29 23
 describe FactoryGirl::Proxy::Create, "when running callbacks" do
@@ -38,7 +32,7 @@
38 32
   subject { FactoryGirl::Proxy::Create.new(proxy_class, [after_create_one, after_create_two, after_build_one]) }
39 33
 
40 34
   it "runs callbacks in the correct order" do
41  
-    subject.result(nil)
  35
+    subject.result(lambda {|instance| instance })
42 36
     callback_result.should == [:after_build_one, :after_create_one, :after_create_two]
43 37
   end
44 38
 end
4  spec/support/shared_examples/proxy.rb
@@ -102,11 +102,11 @@
102 102
   subject        { described_class.new(proxy_class, [callback]) }
103 103
 
104 104
   it "runs the #{callback_name} callback" do
105  
-    subject.result(nil)
  105
+    subject.result(lambda {|instance| instance })
106 106
     callback_instance.should have_received(:foo).once
107 107
   end
108 108
 
109 109
   it "returns the proxy instance" do
110  
-    subject.result(nil).should == instance
  110
+    subject.result(lambda {|instance| instance }).should == instance
111 111
   end
112 112
 end

0 notes on commit 5bbbcb9

Please sign in to comment.
Something went wrong with that request. Please try again.