Skip to content

Commit

Permalink
Honor :parent on factory over block nesting.
Browse files Browse the repository at this point in the history
Closes #280
  • Loading branch information
joshuaclayton committed Jan 23, 2012
1 parent 6282742 commit 4aecfff
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/factory_girl/syntax/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions spec/acceptance/parent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4aecfff

Please sign in to comment.