diff --git a/features/factory_girl_steps.feature b/features/factory_girl_steps.feature index 27ed54f8c..df6e9f98a 100644 --- a/features/factory_girl_steps.feature +++ b/features/factory_girl_steps.feature @@ -60,6 +60,14 @@ Feature: Use step definitions generated by factories | a title | 123 | And there should be 1 user + Scenario: create a titled post with a new author (inherited association) + Given the following titled post exists: + | Title | Author | + | A Post with a Title | ID: 123 | + Then I should find the following for the last post: + | title | author_id | + | A Post with a Title | 123 | + Scenario: create post with and without a category association Given the following users exist: | ID | Name | diff --git a/features/support/factories.rb b/features/support/factories.rb index dbe1757ad..b858cdd2a 100644 --- a/features/support/factories.rb +++ b/features/support/factories.rb @@ -99,6 +99,10 @@ def self.count category end + factory :titled_post, :parent => :post do + title 'A Post with a Title' + end + factory :tag do post end diff --git a/lib/factory_girl/factory.rb b/lib/factory_girl/factory.rb index c457e2fe7..24f741bf7 100644 --- a/lib/factory_girl/factory.rb +++ b/lib/factory_girl/factory.rb @@ -53,7 +53,7 @@ def human_names end def associations - attributes.associations + attributes.associations + parent.associations end # Names for this factory, including aliases. diff --git a/lib/factory_girl/null_factory.rb b/lib/factory_girl/null_factory.rb index 5e812461c..5b27b462f 100644 --- a/lib/factory_girl/null_factory.rb +++ b/lib/factory_girl/null_factory.rb @@ -12,5 +12,6 @@ def compile; end def class_name; end def default_strategy; :create; end def evaluator_class; FactoryGirl::Evaluator; end + def associations; attributes.map(&:association?); end end end diff --git a/spec/factory_girl/factory_spec.rb b/spec/factory_girl/factory_spec.rb index b28ecd1df..1839d8201 100644 --- a/spec/factory_girl/factory_spec.rb +++ b/spec/factory_girl/factory_spec.rb @@ -48,6 +48,15 @@ factory.associations.size.should == 3 end + it "includes associations from the parent factory" do + factory = FactoryGirl::Factory.new(:post) + factory.declare_attribute(FactoryGirl::Declaration::Association.new(:author, {})) + FactoryGirl.register_factory(factory) + child_factory = FactoryGirl::Factory.new(:child_post, :parent => :post) + child_factory.declare_attribute(FactoryGirl::Declaration::Association.new(:editor, {})) + child_factory.associations.size.should == 2 + end + describe "when overriding generated attributes with a hash" do before do @name = :name diff --git a/spec/factory_girl/null_factory_spec.rb b/spec/factory_girl/null_factory_spec.rb index f86ed6128..66b5209f5 100644 --- a/spec/factory_girl/null_factory_spec.rb +++ b/spec/factory_girl/null_factory_spec.rb @@ -10,5 +10,6 @@ its(:class_name) { should be_nil } its(:default_strategy) { should == :create } its(:attributes) { should be_an_instance_of(FactoryGirl::AttributeList) } + its(:associations) { should be_empty } its(:evaluator_class) { should == FactoryGirl::Evaluator } end