mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Only strip strings in step definitions
This allows for cucumber transforms to create different data structrues and still work with factory girl steps. Closes #185
This commit is contained in:
parent
a50c703b7e
commit
05ddd3d6b5
4 changed files with 53 additions and 1 deletions
|
@ -198,3 +198,19 @@ Feature: Use step definitions generated by factories
|
||||||
Then I should find the following for the last user:
|
Then I should find the following for the last user:
|
||||||
| id | name | admin |
|
| id | name | admin |
|
||||||
| 123 | Joe | true |
|
| 123 | Joe | true |
|
||||||
|
|
||||||
|
Scenario: Transform parses string data into array before assigning to an association
|
||||||
|
Given the following tags exist:
|
||||||
|
| name |
|
||||||
|
| funky |
|
||||||
|
| cool |
|
||||||
|
| hip |
|
||||||
|
And the following post exists:
|
||||||
|
| title | tags |
|
||||||
|
| Tagged post | cool, hip |
|
||||||
|
Then the post "Tagged post" should have the following tags:
|
||||||
|
| name |
|
||||||
|
| cool |
|
||||||
|
| hip |
|
||||||
|
And the post "Tagged post" should not have the following tags:
|
||||||
|
| funky |
|
||||||
|
|
|
@ -11,8 +11,30 @@ Then /^there should be (\d+) (.*)$/ do |count, model|
|
||||||
model_class.count.should == count.to_i
|
model_class.count.should == count.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Then /^the post "([^"]*)" should (not )?have the following tags?:$/ do |post_title, negate, table|
|
||||||
|
post = Post.find_by_title!(post_title)
|
||||||
|
|
||||||
|
table.hashes.each do |row|
|
||||||
|
tag = Tag.find_by_name(row[:name])
|
||||||
|
|
||||||
|
if negate
|
||||||
|
post.tags.should_not include(tag)
|
||||||
|
else
|
||||||
|
post.tags.should include(tag)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Transform /^table:(?:.*,)?tags(?:,.*)?$/ do |table|
|
||||||
|
table.map_column!("tags") do |tags|
|
||||||
|
tags.split(',').map {|tag| Tag.find_by_name! tag.strip }
|
||||||
|
end
|
||||||
|
table
|
||||||
|
end
|
||||||
|
|
||||||
Before do
|
Before do
|
||||||
Post.delete_all
|
Post.delete_all
|
||||||
|
Tag.delete_all
|
||||||
User.delete_all
|
User.delete_all
|
||||||
Category.delete_all
|
Category.delete_all
|
||||||
CategoryGroup.delete_all
|
CategoryGroup.delete_all
|
||||||
|
|
|
@ -21,6 +21,11 @@ class CreateSchema < ActiveRecord::Migration
|
||||||
t.string :name
|
t.string :name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table :tags, :force => true do |t|
|
||||||
|
t.integer :post_id
|
||||||
|
t.string :name
|
||||||
|
end
|
||||||
|
|
||||||
create_table :users, :force => true do |t|
|
create_table :users, :force => true do |t|
|
||||||
t.string :name
|
t.string :name
|
||||||
t.boolean :admin, :default => false, :null => false
|
t.boolean :admin, :default => false, :null => false
|
||||||
|
@ -43,6 +48,11 @@ end
|
||||||
class Post < ActiveRecord::Base
|
class Post < ActiveRecord::Base
|
||||||
belongs_to :author, :class_name => 'User'
|
belongs_to :author, :class_name => 'User'
|
||||||
belongs_to :category
|
belongs_to :category
|
||||||
|
has_many :tags
|
||||||
|
end
|
||||||
|
|
||||||
|
class Tag < ActiveRecord::Base
|
||||||
|
belongs_to :post
|
||||||
end
|
end
|
||||||
|
|
||||||
class NonActiveRecord
|
class NonActiveRecord
|
||||||
|
@ -74,6 +84,9 @@ FactoryGirl.define do
|
||||||
category
|
category
|
||||||
end
|
end
|
||||||
|
|
||||||
|
factory :tag do
|
||||||
|
post
|
||||||
|
end
|
||||||
# This is here to ensure that factory step definitions don't raise for a non-AR factory
|
# This is here to ensure that factory step definitions don't raise for a non-AR factory
|
||||||
factory :non_active_record do
|
factory :non_active_record do
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,7 +21,8 @@ module FactoryGirlStepHelpers
|
||||||
private
|
private
|
||||||
|
|
||||||
def process_key_value(key, value)
|
def process_key_value(key, value)
|
||||||
[key.downcase.gsub(' ', '_').to_sym, value.to_s.strip]
|
value = value.strip if value.is_a?(String)
|
||||||
|
[key.downcase.gsub(' ', '_').to_sym, value]
|
||||||
end
|
end
|
||||||
|
|
||||||
class AssociationManager
|
class AssociationManager
|
||||||
|
|
Loading…
Reference in a new issue