Honor :parent on factory over block nesting.

Closes #280
This commit is contained in:
Joshua Clayton 2012-01-23 00:51:51 -05:00
parent 62827420ae
commit 4aecfff126
2 changed files with 25 additions and 1 deletions

View File

@ -24,7 +24,8 @@ module FactoryGirl
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

View File

@ -65,3 +65,26 @@ describe "an instance generated by a factory that inherits from another factory"
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