Skip to content

Commit

Permalink
Let Cucumber steps use inherited associations.
Browse files Browse the repository at this point in the history
By making Factory#associations include the parent's associations.

#292
  • Loading branch information
eostrom authored and joshuaclayton committed Feb 8, 2012
1 parent 13129b0 commit 34c1bf9
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 1 deletion.
8 changes: 8 additions & 0 deletions features/factory_girl_steps.feature
Expand Up @@ -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 |
Expand Down
4 changes: 4 additions & 0 deletions features/support/factories.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/factory_girl/factory.rb
Expand Up @@ -53,7 +53,7 @@ def human_names
end

def associations
attributes.associations
attributes.associations + parent.associations
end

# Names for this factory, including aliases.
Expand Down
1 change: 1 addition & 0 deletions lib/factory_girl/null_factory.rb
Expand Up @@ -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
9 changes: 9 additions & 0 deletions spec/factory_girl/factory_spec.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/factory_girl/null_factory_spec.rb
Expand Up @@ -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

0 comments on commit 34c1bf9

Please sign in to comment.