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:
parent
4f5b775d71
commit
81c9f2c3dd
3 changed files with 41 additions and 10 deletions
|
@ -235,3 +235,7 @@ Feature: Use step definitions generated by factories
|
||||||
Scenario: step definitions work correctly with ORMs that have simple `columns`
|
Scenario: step definitions work correctly with ORMs that have simple `columns`
|
||||||
Given a simple column exists
|
Given a simple column exists
|
||||||
Then there should be 1 SimpleColumn
|
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
|
||||||
|
|
|
@ -73,6 +73,23 @@ class SimpleColumn
|
||||||
end
|
end
|
||||||
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
|
FactoryGirl.define do
|
||||||
# To make sure the step defs work with an email
|
# To make sure the step defs work with an email
|
||||||
sequence :email do |n|
|
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)
|
# This is here to make FG work with ORMs that have `columns => [:name, :admin, :etc]` on the class (Neo4j)
|
||||||
factory :simple_column do
|
factory :simple_column do
|
||||||
end
|
end
|
||||||
|
|
||||||
|
factory :named_attribute_model do
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'factory_girl/step_definitions'
|
require 'factory_girl/step_definitions'
|
||||||
|
|
|
@ -112,17 +112,24 @@ FactoryGirl.factories.each do |factory|
|
||||||
FactoryGirl.create_list(factory.name, count.to_i)
|
FactoryGirl.create_list(factory.name, count.to_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
if factory.build_class.respond_to?(:columns)
|
attribute_names = if factory.build_class.respond_to?(:attribute_names)
|
||||||
factory.build_class.columns.each do |column|
|
factory.build_class.attribute_names
|
||||||
name = column.respond_to?(:name) ? column.name : column.to_s
|
elsif factory.build_class.respond_to?(:columns)
|
||||||
human_column_name = name.downcase.gsub('_', ' ')
|
factory.build_class.columns.map do |column|
|
||||||
Given /^an? #{human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value|
|
column.respond_to?(:name) ? column.name : column.to_s
|
||||||
FactoryGirl.create(factory.name, name => value)
|
end
|
||||||
end
|
else
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
|
||||||
Given /^(\d+) #{human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
|
attribute_names.each do |attribute_name|
|
||||||
FactoryGirl.create_list(factory.name, count.to_i, name => value)
|
human_column_name = attribute_name.downcase.gsub('_', ' ')
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue