Skip to content

Loading…

Make traits work after Factory() was called. #268

Closed
wants to merge 2 commits into from

2 participants

@greyblake

Here is a test to reproduce the issue I faced: 7b0f86c

And that's the fix: 64ad5c0

I guess the problem is Factory() call modifies an instance of factory and sets @evaluator_class.
Next time when Factory@with_traits is called it does self.clone.tap do |factory_with_traits| .. lala .. end. So it clones an existening factory with @evaluator_class already setted.

@joshuaclayton joshuaclayton added a commit that closed this pull request
@joshuaclayton joshuaclayton 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
70a80fd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 33 additions and 1 deletion.
  1. +1 −1 lib/factory_girl/factory.rb
  2. +32 −0 spec/acceptance/traits_spec.rb
View
2 lib/factory_girl/factory.rb
@@ -109,7 +109,7 @@ def class_name #:nodoc:
end
def evaluator_class
- @evaluator_class ||= EvaluatorClassDefiner.new(attributes, callbacks, parent.evaluator_class).evaluator_class
+ EvaluatorClassDefiner.new(attributes, callbacks, parent.evaluator_class).evaluator_class
end
def attributes
View
32 spec/acceptance/traits_spec.rb
@@ -286,3 +286,35 @@
its(:email) { should == "John@example.com" }
its(:combined) { should == "John <John@example.com>" }
end
+
+describe 'traits should work after calling Factory()' do
+ before do
+ define_model('User') do
+ has_many :posts
+ end
+
+ define_model('Post', :user_id => :integer) do
+ belongs_to :user
+ end
+
+ FactoryGirl.define do
+ factory :user do
+ trait :with_post do
+ posts { [Post.new] }
+ end
+ end
+ end
+
+ end
+
+ it "should work as well when Factory() was called" do
+ user = FactoryGirl.create(:user, :with_post)
+ user.posts.should_not be_empty
+
+ Factory(:user)
+
+ user = FactoryGirl.create(:user, :with_post)
+ user.posts.should_not be_empty
+ end
+
+end
Something went wrong with that request. Please try again.