From 2cfc4813fa5c3695157e5bd2e0e277d55363bc51 Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Mon, 29 Mar 2010 17:48:19 -0400 Subject: [PATCH] Cucumber step definitions with blank association columns work intelligently --- features/factory_girl_steps.feature | 12 ++++++++++++ features/support/factories.rb | 14 ++++++++++++++ lib/factory_girl/step_definitions.rb | 1 + 3 files changed, 27 insertions(+) diff --git a/features/factory_girl_steps.feature b/features/factory_girl_steps.feature index 37eced3..7f93eb5 100644 --- a/features/factory_girl_steps.feature +++ b/features/factory_girl_steps.feature @@ -60,6 +60,18 @@ Feature: Use step definitions generated by factories | a title | 123 | And there should be 1 user + Scenario: create post with and without a category association + Given the following users exist: + | ID | Name | + | 123 | Joe | + And the following posts exist: + | Title | Author | Category | + | a title | Name: Joe | Name: programming | + | a title without a category | Name: Joe | | + Then I should find the following for the last post: + | title | category_id | + | a title without a category | | + Scenario: create a user without attributes Given a user exists Then there should be 1 user diff --git a/features/support/factories.rb b/features/support/factories.rb index c8e445e..355fe35 100644 --- a/features/support/factories.rb +++ b/features/support/factories.rb @@ -7,10 +7,15 @@ class CreateSchema < ActiveRecord::Migration def self.up create_table :posts, :force => true do |t| t.integer :author_id + t.integer :category_id t.string :title t.string :body end + create_table :categories, :force => true do |t| + t.string :name + end + create_table :users, :force => true do |t| t.string :name t.boolean :admin, :default => false, :null => false @@ -23,8 +28,12 @@ CreateSchema.suppress_messages { CreateSchema.migrate(:up) } class User < ActiveRecord::Base end +class Category < ActiveRecord::Base +end + class Post < ActiveRecord::Base belongs_to :author, :class_name => 'User' + belongs_to :category end class NonActiveRecord @@ -37,8 +46,13 @@ Factory.define :admin_user, :parent => :user do |f| f.admin true end +Factory.define :category do |f| + f.name "programming" +end + Factory.define :post do |f| f.association :author, :factory => :user + f.association :category end # This is here to ensure that factory step definitions don't raise for a non-AR factory diff --git a/lib/factory_girl/step_definitions.rb b/lib/factory_girl/step_definitions.rb index 620d6a9..75bd082 100644 --- a/lib/factory_girl/step_definitions.rb +++ b/lib/factory_girl/step_definitions.rb @@ -1,6 +1,7 @@ module FactoryGirlStepHelpers def convert_association_string_to_instance(factory_name, assignment) attribute, value = assignment.split(':', 2) + return if value.blank? attributes = convert_human_hash_to_attribute_hash(attribute => value.strip) factory = Factory.factory_by_name(factory_name) model_class = factory.build_class