1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Make step definitions more ORM agnostic by preferring .attribute_names

Support for .attribute_names
* ActiveRecord starting in v3.1.0
* ActiveAttr starting in v0.5.0
This commit is contained in:
Chris Griego 2012-03-11 23:27:14 -05:00 committed by Joshua Clayton
parent 4f5b775d71
commit 81c9f2c3dd
3 changed files with 41 additions and 10 deletions

View file

@ -235,3 +235,7 @@ Feature: Use step definitions generated by factories
Scenario: step definitions work correctly with ORMs that have simple `columns`
Given a simple column exists
Then there should be 1 SimpleColumn
Scenario: step definitions work correctly with model classes that simply have attribute names
Given a named attribute model exists with a title of "a fun title"
Then there should be 1 NamedAttributeModel

View file

@ -73,6 +73,23 @@ class SimpleColumn
end
end
class NamedAttributeModel
def self.attribute_names
%w(title)
end
attr_accessor :title
def save!
@@count ||= 0
@@count += 1
end
def self.count
@@count
end
end
FactoryGirl.define do
# To make sure the step defs work with an email
sequence :email do |n|
@ -113,6 +130,9 @@ FactoryGirl.define do
# This is here to make FG work with ORMs that have `columns => [:name, :admin, :etc]` on the class (Neo4j)
factory :simple_column do
end
factory :named_attribute_model do
end
end
require 'factory_girl/step_definitions'

View file

@ -112,17 +112,24 @@ FactoryGirl.factories.each do |factory|
FactoryGirl.create_list(factory.name, count.to_i)
end
if factory.build_class.respond_to?(:columns)
factory.build_class.columns.each do |column|
name = column.respond_to?(:name) ? column.name : column.to_s
human_column_name = name.downcase.gsub('_', ' ')
Given /^an? #{human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value|
FactoryGirl.create(factory.name, name => value)
end
attribute_names = if factory.build_class.respond_to?(:attribute_names)
factory.build_class.attribute_names
elsif factory.build_class.respond_to?(:columns)
factory.build_class.columns.map do |column|
column.respond_to?(:name) ? column.name : column.to_s
end
else
[]
end
Given /^(\d+) #{human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
FactoryGirl.create_list(factory.name, count.to_i, name => value)
end
attribute_names.each do |attribute_name|
human_column_name = attribute_name.downcase.gsub('_', ' ')
Given /^an? #{human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value|
FactoryGirl.create(factory.name, attribute_name => value)
end
Given /^(\d+) #{human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
FactoryGirl.create_list(factory.name, count.to_i, attribute_name => value)
end
end
end