Skip to content

Commit

Permalink
Properly support pluralized words in generated step definitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristandunn committed Jun 7, 2010
1 parent 389ec8f commit 1d42b18
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
9 changes: 9 additions & 0 deletions features/factory_girl_steps.feature
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,12 @@ Feature: Use step definitions generated by factories
Then I should find the following for the last user:
| name | admin |
| Mike | false |

Scenario: Properly pluralize words
Given the following categories exist:
| name |
| Bed |
| Test Drive |
And 2 categories exist
And 2 categories exist with a name of "Future"
Then there should be 6 categories
1 change: 1 addition & 0 deletions features/step_definitions/database_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
Before do
Post.delete_all
User.delete_all
Category.delete_all
end
7 changes: 3 additions & 4 deletions lib/factory_girl/step_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def convert_human_hash_to_attribute_hash(human_hash, associations = [])
World(FactoryGirlStepHelpers)

Factory.factories.values.each do |factory|
# TODO: support irregular pluralizations
Given /^the following #{factory.human_name}s? exists?:$/ do |table|
Given /^the following (?:#{factory.human_name}|#{factory.human_name.pluralize}) exists?:$/ do |table|
table.hashes.each do |human_hash|
attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
Factory.create(factory.factory_name, attributes)
Expand All @@ -35,7 +34,7 @@ def convert_human_hash_to_attribute_hash(human_hash, associations = [])
Factory(factory.factory_name)
end

Given /^(\d+) #{factory.human_name}s exist$/ do |count|
Given /^(\d+) #{factory.human_name.pluralize} exist$/ do |count|
count.to_i.times { Factory(factory.factory_name) }
end

Expand All @@ -46,7 +45,7 @@ def convert_human_hash_to_attribute_hash(human_hash, associations = [])
Factory(factory.factory_name, column.name => value)
end

Given /^(\d+) #{factory.human_name}s exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
Given /^(\d+) #{factory.human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
count.to_i.times { Factory(factory.factory_name, column.name => value) }
end
end
Expand Down

3 comments on commit 1d42b18

@cgriego
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I had just sat down to write a patch. Now I'll just upgrade our project to the latest version. :)

Question: Doesn't this add an undeclared runtime dependency on activesupport?

@tristandunn
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's been annoying me for a while. It does add an undeclared dependency, but odds are you're using these step definitions with Rails. Perhaps this should be moved to the new factory_girl_rails gem instead.

@cgriego
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach I was going to take with the patch was to define factory#plural_human_name that internally used #pluralize if it was defined or drop-back to dumb "s" concatenation if it was undefined. That way activesupport is a non-breaking optional dependency.

Please sign in to comment.