Skip to content

Commit

Permalink
Only strip strings in step definitions
Browse files Browse the repository at this point in the history
This allows for cucumber transforms to create different data structrues
and still work with factory girl steps.

Closes #185
  • Loading branch information
joshuaclayton committed Aug 22, 2011
1 parent a50c703 commit 05ddd3d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
16 changes: 16 additions & 0 deletions features/factory_girl_steps.feature
Expand Up @@ -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 |
22 changes: 22 additions & 0 deletions features/step_definitions/database_steps.rb
Expand Up @@ -11,8 +11,30 @@
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
Expand Down
13 changes: 13 additions & 0 deletions features/support/factories.rb
Expand Up @@ -21,6 +21,11 @@ def self.up
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
Expand All @@ -43,6 +48,11 @@ class Category < ActiveRecord::Base
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
Expand Down Expand Up @@ -74,6 +84,9 @@ class NonActiveRecord
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
Expand Down
3 changes: 2 additions & 1 deletion lib/factory_girl/step_definitions.rb
Expand Up @@ -21,7 +21,8 @@ def attributes(strategy = CreateAttributes)
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
Expand Down

0 comments on commit 05ddd3d

Please sign in to comment.