Fixed issues with some attributes being skipped and added support for linked associations in step definitions
This commit is contained in:
parent
15f36c8fc9
commit
33b61c3bfb
|
@ -0,0 +1 @@
|
||||||
|
default: -r features features
|
|
@ -18,3 +18,25 @@ Feature: Use step definitions generated by factories
|
||||||
| title | three |
|
| title | three |
|
||||||
| body | third |
|
| body | third |
|
||||||
And there should be 3 posts
|
And there should be 3 posts
|
||||||
|
|
||||||
|
Scenario: create a post with a new author
|
||||||
|
Given the following post exists:
|
||||||
|
| Title | a title |
|
||||||
|
| Author | ID: 123 |
|
||||||
|
Then I should find the following for the last post:
|
||||||
|
| title | a title |
|
||||||
|
| author_id | 123 |
|
||||||
|
And I should find the following for the last user:
|
||||||
|
| id | 123 |
|
||||||
|
|
||||||
|
Scenario: create a post with an existing author
|
||||||
|
Given the following user exists:
|
||||||
|
| ID | 123 |
|
||||||
|
| Name | Joe |
|
||||||
|
And the following post exists:
|
||||||
|
| Title | a title |
|
||||||
|
| Author | Name: Joe |
|
||||||
|
Then I should find the following for the last post:
|
||||||
|
| title | a title |
|
||||||
|
| author_id | 123 |
|
||||||
|
And there should be 1 user
|
||||||
|
|
|
@ -1,15 +1,18 @@
|
||||||
Then /^I should find the following for the last post:$/ do |table|
|
Then /^I should find the following for the last (.*):$/ do |model, table|
|
||||||
last_post = Post.last or raise "No posts exist"
|
model_class = model.camelize.constantize
|
||||||
attributes = table.rows.inject({}) {|res, (key, value)| res.merge(key => value) }
|
last_instance = model_class.last or raise "No #{model.pluralize} exist"
|
||||||
|
attributes = table.raw.inject({}) {|res, (key, value)| res.merge(key => value) }
|
||||||
attributes.each do |key, value|
|
attributes.each do |key, value|
|
||||||
last_post.attributes[key].should == value
|
last_instance.attributes[key].to_s.should == value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Then /^there should be (\d+) posts$/ do |count|
|
Then /^there should be (\d+) (.*)$/ do |count, model|
|
||||||
Post.count.should == count.to_i
|
model_class = model.singularize.camelize.constantize
|
||||||
|
model_class.count.should == count.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
Before do
|
Before do
|
||||||
Post.delete_all
|
Post.delete_all
|
||||||
|
User.delete_all
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,18 +6,31 @@ ActiveRecord::Base.establish_connection(
|
||||||
class CreateSchema < ActiveRecord::Migration
|
class CreateSchema < ActiveRecord::Migration
|
||||||
def self.up
|
def self.up
|
||||||
create_table :posts, :force => true do |t|
|
create_table :posts, :force => true do |t|
|
||||||
|
t.integer :author_id
|
||||||
t.string :title
|
t.string :title
|
||||||
t.string :body
|
t.string :body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table :users, :force => true do |t|
|
||||||
|
t.string :name
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
CreateSchema.suppress_messages { CreateSchema.migrate(:up) }
|
CreateSchema.suppress_messages { CreateSchema.migrate(:up) }
|
||||||
|
|
||||||
|
class User < ActiveRecord::Base
|
||||||
|
end
|
||||||
|
|
||||||
class Post < ActiveRecord::Base
|
class Post < ActiveRecord::Base
|
||||||
|
belongs_to :author, :class_name => 'User'
|
||||||
|
end
|
||||||
|
|
||||||
|
Factory.define :user do |f|
|
||||||
end
|
end
|
||||||
|
|
||||||
Factory.define :post do |f|
|
Factory.define :post do |f|
|
||||||
|
f.association :author, :factory => :user
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'factory_girl/step_definitions'
|
require 'factory_girl/step_definitions'
|
||||||
|
|
|
@ -3,6 +3,8 @@ class Factory
|
||||||
|
|
||||||
class Association < Attribute #:nodoc:
|
class Association < Attribute #:nodoc:
|
||||||
|
|
||||||
|
attr_reader :factory
|
||||||
|
|
||||||
def initialize(name, factory, overrides)
|
def initialize(name, factory, overrides)
|
||||||
super(name)
|
super(name)
|
||||||
@factory = factory
|
@factory = factory
|
||||||
|
|
|
@ -305,6 +305,10 @@ class Factory
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def associations
|
||||||
|
attributes.select {|attribute| attribute.is_a?(Attribute::Association) }
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def class_for (class_or_to_s)
|
def class_for (class_or_to_s)
|
||||||
|
|
|
@ -1,14 +1,25 @@
|
||||||
module FactoryGirlStepHelpers
|
module FactoryGirlStepHelpers
|
||||||
def convert_ast_table_to_attribute_hash(table)
|
def convert_vertical_table_to_hash(table)
|
||||||
table.rows.inject({}) do |result, (human_key, value)|
|
table.raw.inject({}) do |result, (key, value)|
|
||||||
key = human_key.downcase.gsub(' ', '_').to_sym
|
|
||||||
result.merge(key => value)
|
result.merge(key => value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def convert_human_hash_to_attribute_hash(human_hash)
|
def convert_association_string_to_instance(factory_name, assignment)
|
||||||
|
attribute, value = assignment.split(':', 2)
|
||||||
|
attributes = convert_human_hash_to_attribute_hash(attribute => value.strip)
|
||||||
|
factory = Factory.factory_by_name(factory_name)
|
||||||
|
model_class = factory.build_class
|
||||||
|
model_class.find(:first, :conditions => attributes) or
|
||||||
|
Factory(factory_name, attributes)
|
||||||
|
end
|
||||||
|
|
||||||
|
def convert_human_hash_to_attribute_hash(human_hash, associations = [])
|
||||||
human_hash.inject({}) do |attribute_hash, (human_key, value)|
|
human_hash.inject({}) do |attribute_hash, (human_key, value)|
|
||||||
key = human_key.downcase.gsub(' ', '_').to_sym
|
key = human_key.downcase.gsub(' ', '_').to_sym
|
||||||
|
if association = associations.detect {|association| association.name == key }
|
||||||
|
value = convert_association_string_to_instance(association.factory, value)
|
||||||
|
end
|
||||||
attribute_hash.merge(key => value)
|
attribute_hash.merge(key => value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -18,14 +29,15 @@ World(FactoryGirlStepHelpers)
|
||||||
|
|
||||||
Factory.factories.values.each do |factory|
|
Factory.factories.values.each do |factory|
|
||||||
Given "the following #{factory.human_name} exists:" do |table|
|
Given "the following #{factory.human_name} exists:" do |table|
|
||||||
attributes = convert_ast_table_to_attribute_hash(table)
|
human_hash = convert_vertical_table_to_hash(table)
|
||||||
|
attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
|
||||||
Factory.create(factory.factory_name, attributes)
|
Factory.create(factory.factory_name, attributes)
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: support irregular pluralizations
|
# TODO: support irregular pluralizations
|
||||||
Given "the following #{factory.human_name}s exist:" do |table|
|
Given "the following #{factory.human_name}s exist:" do |table|
|
||||||
table.hashes.each do |human_hash|
|
table.hashes.each do |human_hash|
|
||||||
attributes = convert_human_hash_to_attribute_hash(human_hash)
|
attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
|
||||||
Factory.create(factory.factory_name, attributes)
|
Factory.create(factory.factory_name, attributes)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,6 +12,10 @@ describe Factory::Attribute::Association do
|
||||||
@attr.name.should == @name
|
@attr.name.should == @name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should have a factory" do
|
||||||
|
@attr.factory.should == @factory
|
||||||
|
end
|
||||||
|
|
||||||
it "should tell the proxy to associate when being added to a proxy" do
|
it "should tell the proxy to associate when being added to a proxy" do
|
||||||
proxy = "proxy"
|
proxy = "proxy"
|
||||||
stub(proxy).associate
|
stub(proxy).associate
|
||||||
|
|
|
@ -134,6 +134,16 @@ describe Factory do
|
||||||
factory.attributes.should include(attr)
|
factory.attributes.should include(attr)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should return associations" do
|
||||||
|
factory = Factory.new(:post)
|
||||||
|
factory.association(:author)
|
||||||
|
factory.association(:editor)
|
||||||
|
factory.associations.each do |association|
|
||||||
|
association.should be_a(Factory::Attribute::Association)
|
||||||
|
end
|
||||||
|
factory.associations.size.should == 2
|
||||||
|
end
|
||||||
|
|
||||||
it "should add an association with overrides" do
|
it "should add an association with overrides" do
|
||||||
factory = Factory.new(:post)
|
factory = Factory.new(:post)
|
||||||
name = :user
|
name = :user
|
||||||
|
|
Loading…
Reference in New Issue