diff --git a/lib/factory_girl/syntax/default.rb b/lib/factory_girl/syntax/default.rb index a89904abb..36372914c 100644 --- a/lib/factory_girl/syntax/default.rb +++ b/lib/factory_girl/syntax/default.rb @@ -24,7 +24,8 @@ def factory(name, options = {}, &block) FactoryGirl.register_factory(factory) proxy.child_factories.each do |(child_name, child_options, child_block)| - factory(child_name, child_options.merge(:parent => name), &child_block) + parent_factory = child_options.delete(:parent) || name + factory(child_name, child_options.merge(:parent => parent_factory), &child_block) end end diff --git a/spec/acceptance/parent_spec.rb b/spec/acceptance/parent_spec.rb index 753405cf2..36ee45f6d 100644 --- a/spec/acceptance/parent_spec.rb +++ b/spec/acceptance/parent_spec.rb @@ -65,3 +65,26 @@ end end +describe "nested factories with different parents" do + before do + define_model("User", :name => :string) + + FactoryGirl.define do + factory :user do + name "Basic User" + + factory :male_user do + name "John Doe" + end + + factory :uppercase_male_user, :parent => :male_user do + after_build {|user| user.name = user.name.upcase } + end + end + end + end + + it "honors :parent over the factory block nesting" do + FactoryGirl.build(:uppercase_male_user).name.should == "JOHN DOE" + end +end