Skip to content
This repository
Browse code

Reset evaluator class when cloning a FactoryGirl::Factory

Applying traits inline modifies the evaluator class, so it needs to be
reset when cloning in order to ensure that the attributes are correct.

Closes #268
  • Loading branch information...
commit 70a80fde5845749ae8404bd70b81c5d6183f68fd 1 parent a883315
Joshua Clayton authored January 16, 2012
1  lib/factory_girl/factory.rb
@@ -152,6 +152,7 @@ def parent
152 152
     def initialize_copy(source)
153 153
       super
154 154
       @definition = @definition.clone
  155
+      @evaluator_class = nil
155 156
     end
156 157
   end
157 158
 end
26  spec/acceptance/traits_spec.rb
@@ -286,3 +286,29 @@
286 286
   its(:email)    { should == "John@example.com" }
287 287
   its(:combined) { should == "John <John@example.com>" }
288 288
 end
  289
+
  290
+describe "applying inline traits" do
  291
+  before do
  292
+    define_model("User") do
  293
+      has_many :posts
  294
+    end
  295
+
  296
+    define_model("Post", :user_id => :integer) do
  297
+      belongs_to :user
  298
+    end
  299
+
  300
+    FactoryGirl.define do
  301
+      factory :user do
  302
+        trait :with_post do
  303
+          posts { [ Post.new ] }
  304
+        end
  305
+      end
  306
+    end
  307
+  end
  308
+
  309
+  it "applies traits only to the instance generated for that call" do
  310
+    FactoryGirl.create(:user, :with_post).posts.should_not be_empty
  311
+    FactoryGirl.create(:user).posts.should be_empty
  312
+    FactoryGirl.create(:user, :with_post).posts.should_not be_empty
  313
+  end
  314
+end

0 notes on commit 70a80fd

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