Permalink
Browse files

Honor :parent on factory over block nesting.

Closes #280
  • Loading branch information...
1 parent 6282742 commit 4aecfff1262b6a6bb642b040d87c7f06450f74fe @joshuaclayton joshuaclayton committed Jan 23, 2012
Showing with 25 additions and 1 deletion.
  1. +2 −1 lib/factory_girl/syntax/default.rb
  2. +23 −0 spec/acceptance/parent_spec.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
@@ -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.