Skip to content
This repository
Browse code

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 authored joshuaclayton committed
4  features/factory_girl_steps.feature
@@ -235,3 +235,7 @@ Feature: Use step definitions generated by factories
235 235
   Scenario: step definitions work correctly with ORMs that have simple `columns`
236 236
     Given a simple column exists
237 237
     Then there should be 1 SimpleColumn
  238
+
  239
+  Scenario: step definitions work correctly with model classes that simply have attribute names
  240
+    Given a named attribute model exists with a title of "a fun title"
  241
+    Then there should be 1 NamedAttributeModel
20  features/support/factories.rb
@@ -73,6 +73,23 @@ def self.count
73 73
   end
74 74
 end
75 75
 
  76
+class NamedAttributeModel
  77
+  def self.attribute_names
  78
+    %w(title)
  79
+  end
  80
+
  81
+  attr_accessor :title
  82
+
  83
+  def save!
  84
+    @@count ||= 0
  85
+    @@count += 1
  86
+  end
  87
+
  88
+  def self.count
  89
+    @@count
  90
+  end
  91
+end
  92
+
76 93
 FactoryGirl.define do
77 94
   # To make sure the step defs work with an email
78 95
   sequence :email do |n|
@@ -113,6 +130,9 @@ def self.count
113 130
   # This is here to make FG work with ORMs that have `columns => [:name, :admin, :etc]` on the class (Neo4j)
114 131
   factory :simple_column do
115 132
   end
  133
+
  134
+  factory :named_attribute_model do
  135
+  end
116 136
 end
117 137
 
118 138
 require 'factory_girl/step_definitions'
27  lib/factory_girl/step_definitions.rb
@@ -112,17 +112,24 @@ def initialize(human_hash_to_attributes_hash, key, value)
112 112
       FactoryGirl.create_list(factory.name, count.to_i)
113 113
     end
114 114
 
115  
-    if factory.build_class.respond_to?(:columns)
116  
-      factory.build_class.columns.each do |column|
117  
-        name = column.respond_to?(:name) ? column.name : column.to_s
118  
-        human_column_name = name.downcase.gsub('_', ' ')
119  
-        Given /^an? #{human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value|
120  
-          FactoryGirl.create(factory.name, name => value)
121  
-        end
  115
+    attribute_names = if factory.build_class.respond_to?(:attribute_names)
  116
+      factory.build_class.attribute_names
  117
+    elsif factory.build_class.respond_to?(:columns)
  118
+      factory.build_class.columns.map do |column|
  119
+        column.respond_to?(:name) ? column.name : column.to_s
  120
+      end
  121
+    else
  122
+      []
  123
+    end
122 124
 
123  
-        Given /^(\d+) #{human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
124  
-          FactoryGirl.create_list(factory.name, count.to_i, name => value)
125  
-        end
  125
+    attribute_names.each do |attribute_name|
  126
+      human_column_name = attribute_name.downcase.gsub('_', ' ')
  127
+      Given /^an? #{human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value|
  128
+        FactoryGirl.create(factory.name, attribute_name => value)
  129
+      end
  130
+
  131
+      Given /^(\d+) #{human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
  132
+        FactoryGirl.create_list(factory.name, count.to_i, attribute_name => value)
126 133
       end
127 134
     end
128 135
   end

0 notes on commit 81c9f2c

Please sign in to comment.
Something went wrong with that request. Please try again.