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:
|
||||
| id | name | admin |
|
||||
| 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
|
||||
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
|
||||
Post.delete_all
|
||||
Tag.delete_all
|
||||
User.delete_all
|
||||
Category.delete_all
|
||||
CategoryGroup.delete_all
|
||||
|
|
|
@ -21,6 +21,11 @@ class CreateSchema < ActiveRecord::Migration
|
|||
t.string :name
|
||||
end
|
||||
|
||||
create_table :tags, :force => true do |t|
|
||||
t.integer :post_id
|
||||
t.string :name
|
||||
end
|
||||
|
||||
create_table :users, :force => true do |t|
|
||||
t.string :name
|
||||
t.boolean :admin, :default => false, :null => false
|
||||
|
@ -43,6 +48,11 @@ end
|
|||
class Post < ActiveRecord::Base
|
||||
belongs_to :author, :class_name => 'User'
|
||||
belongs_to :category
|
||||
has_many :tags
|
||||
end
|
||||
|
||||
class Tag < ActiveRecord::Base
|
||||
belongs_to :post
|
||||
end
|
||||
|
||||
class NonActiveRecord
|
||||
|
@ -74,6 +84,9 @@ FactoryGirl.define do
|
|||
category
|
||||
end
|
||||
|
||||
factory :tag do
|
||||
post
|
||||
end
|
||||
# This is here to ensure that factory step definitions don't raise for a non-AR factory
|
||||
factory :non_active_record do
|
||||
end
|
||||
|
|
|
@ -21,7 +21,8 @@ module FactoryGirlStepHelpers
|
|||
private
|
||||
|
||||
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
|
||||
|
||||
class AssociationManager
|
||||
|
|
Loading…
Reference in a new issue