mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Switched to use horizontal Cucumber tables instead of vertical
This commit is contained in:
parent
33b61c3bfb
commit
ba6cfea5c7
3 changed files with 20 additions and 32 deletions
|
@ -2,11 +2,11 @@ Feature: Use step definitions generated by factories
|
|||
|
||||
Scenario: create a post and verify its attributes
|
||||
Given the following post exists:
|
||||
| Title | a fun title |
|
||||
| Body | here is the content |
|
||||
| Title | Body |
|
||||
| a fun title | here is the content |
|
||||
Then I should find the following for the last post:
|
||||
| title | a fun title |
|
||||
| body | here is the content |
|
||||
| title | body |
|
||||
| a fun title | here is the content |
|
||||
|
||||
Scenario: create several posts
|
||||
Given the following posts exist:
|
||||
|
@ -15,28 +15,29 @@ Feature: Use step definitions generated by factories
|
|||
| two | second |
|
||||
| three | third |
|
||||
Then I should find the following for the last post:
|
||||
| title | three |
|
||||
| body | third |
|
||||
| title | body |
|
||||
| three | third |
|
||||
And there should be 3 posts
|
||||
|
||||
Scenario: create a post with a new author
|
||||
Given the following post exists:
|
||||
| Title | a title |
|
||||
| Author | ID: 123 |
|
||||
| Title | Author |
|
||||
| a title | ID: 123 |
|
||||
Then I should find the following for the last post:
|
||||
| title | a title |
|
||||
| author_id | 123 |
|
||||
| title | author_id |
|
||||
| a title | 123 |
|
||||
And I should find the following for the last user:
|
||||
| id | 123 |
|
||||
| id |
|
||||
| 123 |
|
||||
|
||||
Scenario: create a post with an existing author
|
||||
Given the following user exists:
|
||||
| ID | 123 |
|
||||
| Name | Joe |
|
||||
| ID | Name |
|
||||
| 123 | Joe |
|
||||
And the following post exists:
|
||||
| Title | a title |
|
||||
| Author | Name: Joe |
|
||||
| Title | Author |
|
||||
| a title | Name: Joe |
|
||||
Then I should find the following for the last post:
|
||||
| title | a title |
|
||||
| author_id | 123 |
|
||||
| title | author_id |
|
||||
| a title | 123 |
|
||||
And there should be 1 user
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
Then /^I should find the following for the last (.*):$/ do |model, table|
|
||||
model_class = model.camelize.constantize
|
||||
last_instance = model_class.last or raise "No #{model.pluralize} exist"
|
||||
attributes = table.raw.inject({}) {|res, (key, value)| res.merge(key => value) }
|
||||
attributes.each do |key, value|
|
||||
table.hashes.first.each do |key, value|
|
||||
last_instance.attributes[key].to_s.should == value
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,10 +1,4 @@
|
|||
module FactoryGirlStepHelpers
|
||||
def convert_vertical_table_to_hash(table)
|
||||
table.raw.inject({}) do |result, (key, value)|
|
||||
result.merge(key => value)
|
||||
end
|
||||
end
|
||||
|
||||
def convert_association_string_to_instance(factory_name, assignment)
|
||||
attribute, value = assignment.split(':', 2)
|
||||
attributes = convert_human_hash_to_attribute_hash(attribute => value.strip)
|
||||
|
@ -28,14 +22,8 @@ end
|
|||
World(FactoryGirlStepHelpers)
|
||||
|
||||
Factory.factories.values.each do |factory|
|
||||
Given "the following #{factory.human_name} exists:" do |table|
|
||||
human_hash = convert_vertical_table_to_hash(table)
|
||||
attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
|
||||
Factory.create(factory.factory_name, attributes)
|
||||
end
|
||||
|
||||
# TODO: support irregular pluralizations
|
||||
Given "the following #{factory.human_name}s exist:" do |table|
|
||||
Given /^the following #{factory.human_name}s? 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)
|
||||
|
|
Loading…
Add table
Reference in a new issue