Skip to content

Commit

Permalink
Reset evaluator class when cloning a FactoryGirl::Factory
Browse files Browse the repository at this point in the history
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
joshuaclayton committed Jan 16, 2012
1 parent a883315 commit 70a80fd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/factory_girl/factory.rb
Expand Up @@ -152,6 +152,7 @@ def parent
def initialize_copy(source)
super
@definition = @definition.clone
@evaluator_class = nil
end
end
end
26 changes: 26 additions & 0 deletions spec/acceptance/traits_spec.rb
Expand Up @@ -286,3 +286,29 @@
its(:email) { should == "John@example.com" }
its(:combined) { should == "John <John@example.com>" }
end

describe "applying inline traits" 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 "applies traits only to the instance generated for that call" do
FactoryGirl.create(:user, :with_post).posts.should_not be_empty
FactoryGirl.create(:user).posts.should be_empty
FactoryGirl.create(:user, :with_post).posts.should_not be_empty
end
end

0 comments on commit 70a80fd

Please sign in to comment.