Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 81c9f2c3dd81b2d709cb08d23673eec81b521c94 1 parent 4f5b775
Chris Griego cgriego authored joshuaclayton committed
4 features/factory_girl_steps.feature
View
@@ -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
20 features/support/factories.rb
View
@@ -73,6 +73,23 @@ def self.count
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 @@ def self.count
# 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'
27 lib/factory_girl/step_definitions.rb
View
@@ -112,17 +112,24 @@ def initialize(human_hash_to_attributes_hash, key, value)
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
Please sign in to comment.
Something went wrong with that request. Please try again.