Skip to content

Commit

Permalink
fix FactoryRunner#run bug re: uncompiled factory
Browse files Browse the repository at this point in the history
  • Loading branch information
barunio authored and joshuaclayton committed Mar 9, 2012
1 parent 87c1ddb commit 68ca50f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/factory_girl/factory_runner.rb
Expand Up @@ -16,6 +16,8 @@ def run(strategy_override = nil, &block)
strategy_override ||= @strategy
factory = FactoryGirl.factory_by_name(@name)

factory.compile

if @traits.any?
factory = factory.with_traits(@traits)
end
Expand Down
24 changes: 24 additions & 0 deletions spec/acceptance/traits_spec.rb
Expand Up @@ -395,3 +395,27 @@
FactoryGirl.build(:extended_declined_user, :accepted, :status => "completely overridden").status.should == "completely overridden"
end
end

describe "making sure the factory is properly compiled the first time we want to instantiate it" do
before do
define_model("User", :role => :string, :gender => :string, :age => :integer)

FactoryGirl.define do
factory :user do
trait(:female) { gender "female" }
trait(:admin) { role "admin" }

factory :female_user do
female
end
end
end
end

it "can honor traits on the very first call" do
user = FactoryGirl.build(:female_user, :admin, :age => 30)
user.gender.should == 'female'
user.age.should == 30
user.role.should == 'admin'
end
end
2 changes: 2 additions & 0 deletions spec/support/shared_examples/strategy.rb
Expand Up @@ -27,6 +27,7 @@ def association_named(name, strategy, overrides)

before do
FactoryGirl.stubs(:factory_by_name => factory)
factory.stubs(:compile)
factory.stubs(:run)
end

Expand All @@ -52,6 +53,7 @@ def association_named(name, overrides)

before do
FactoryGirl.stubs(:factory_by_name => factory)
factory.stubs(:compile)
factory.stubs(:run)
end

Expand Down

0 comments on commit 68ca50f

Please sign in to comment.