From 05ddd3d6b53da057eb2b9faf5897d27562391622 Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Mon, 22 Aug 2011 13:16:32 -0400 Subject: [PATCH] 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 --- features/factory_girl_steps.feature | 16 +++++++++++++++ features/step_definitions/database_steps.rb | 22 +++++++++++++++++++++ features/support/factories.rb | 13 ++++++++++++ lib/factory_girl/step_definitions.rb | 3 ++- 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/features/factory_girl_steps.feature b/features/factory_girl_steps.feature index edf6ea0..a52974b 100644 --- a/features/factory_girl_steps.feature +++ b/features/factory_girl_steps.feature @@ -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 | diff --git a/features/step_definitions/database_steps.rb b/features/step_definitions/database_steps.rb index 966332f..31bc169 100644 --- a/features/step_definitions/database_steps.rb +++ b/features/step_definitions/database_steps.rb @@ -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 diff --git a/features/support/factories.rb b/features/support/factories.rb index 16ffa5e..bc91a4d 100644 --- a/features/support/factories.rb +++ b/features/support/factories.rb @@ -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 diff --git a/lib/factory_girl/step_definitions.rb b/lib/factory_girl/step_definitions.rb index c1d945e..7eef1ff 100644 --- a/lib/factory_girl/step_definitions.rb +++ b/lib/factory_girl/step_definitions.rb @@ -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