Skip to content

Commit

Permalink
Refactored Factory#association to use a new Attribute class
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Jan 2, 2009
1 parent 56e31ca commit 8e33e61
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 57 deletions.
1 change: 1 addition & 0 deletions lib/factory_girl.rb
Expand Up @@ -7,6 +7,7 @@
require 'factory_girl/attribute'
require 'factory_girl/attribute/static'
require 'factory_girl/attribute/dynamic'
require 'factory_girl/attribute/association'
require 'factory_girl/sequence'
require 'factory_girl/aliases'

Expand Down
18 changes: 18 additions & 0 deletions lib/factory_girl/attribute/association.rb
@@ -0,0 +1,18 @@
class Factory
class Attribute #:nodoc:

class Association < Attribute #:nodoc:

def initialize(name, factory, overrides)
super(name)
@factory = factory
@overrides = overrides
end

def add_to(proxy)
proxy.associate(name, @factory, @overrides)
end
end

end
end
7 changes: 2 additions & 5 deletions lib/factory_girl/factory.rb
Expand Up @@ -122,11 +122,8 @@ def method_missing (name, *args, &block)
# name of the factory. For example, a "user" association will by
# default use the "user" factory.
def association (name, options = {})
name = name.to_sym
options = symbolize_keys(options)
association_factory = options[:factory] || name

add_attribute(name) {|a| a.association(association_factory) }
factory_name = options.delete(:factory) || name
@attributes << Attribute::Association.new(name, factory_name, options)
end

# Generates and returns a Hash of attributes from this factory. Attributes
Expand Down
31 changes: 31 additions & 0 deletions test/association_attribute_test.rb
@@ -0,0 +1,31 @@
require(File.join(File.dirname(__FILE__), 'test_helper'))

class AssociationAttributeTest < Test::Unit::TestCase

context "an association" do
setup do
@name = :author
@factory = :user
@overrides = { :first_name => 'John' }
@attr = Factory::Attribute::Association.new(@name,
@factory,
@overrides)
end

should "have a name" do
assert_equal @name, @attr.name
end

should "tell the proxy to associate when being added to a proxy" do
proxy = mock('proxy')
proxy.expects(:associate).with(@name, @factory, @overrides)
@attr.add_to(proxy)
end
end

should "convert names to symbols" do
assert_equal :name,
Factory::Attribute::Association.new('name', :user, {}).name
end

end
93 changes: 41 additions & 52 deletions test/factory_test.rb
Expand Up @@ -2,24 +2,6 @@

class FactoryTest < Test::Unit::TestCase

def self.should_instantiate_class
should "instantiate the build class" do
assert_kind_of @class, @instance
end

should "assign attributes on the instance" do
assert_equal @first_name, @instance.first_name
assert_equal @last_name, @instance.last_name
end

should "override attributes using the passed hash" do
@value = 'Davis'
@instance = @factory.run(Factory::Proxy::Build,
:first_name => @value)
assert_equal @value, @instance.first_name
end
end

context "defining a factory" do
setup do
@name = :user
Expand Down Expand Up @@ -131,44 +113,51 @@ def self.should_instantiate_class
end
end

context "when adding an association without a factory name" do
setup do
@factory = Factory.new(:post)
@name = :user
@factory.association(@name)
Post.any_instance.stubs(:user=)
Factory.stubs(:create)
end

should "add an attribute with the name of the association" do
result = @factory.run(Factory::Proxy::AttributesFor, {})
assert result.key?(@name)
end

should "create a block that builds the association" do
Factory.expects(:create).with(@name, {})
@factory.run(Factory::Proxy::Build, {})
end
should "add an association without a factory name or overrides" do
factory = Factory.new(:post)
name = :user
attr = 'attribute'
Factory::Attribute::Association.
expects(:new).
with(name, name, {}).
returns(attr)
factory.association(name)
assert factory.attributes.include?(attr)
end

context "when adding an association with a factory name" do
setup do
@factory = Factory.new(:post)
@name = :author
@factory_name = :user
@factory.association(@name, :factory => @factory_name)
Factory.stubs(:create)
end
should "add an association with overrides" do
factory = Factory.new(:post)
name = :user
attr = 'attribute'
overrides = { :first_name => 'Ben' }
Factory::Attribute::Association.
expects(:new).
with(name, name, overrides).
returns(attr)
factory.association(name, overrides)
assert factory.attributes.include?(attr)
end

should "add an attribute with the name of the association" do
result = @factory.run(Factory::Proxy::AttributesFor, {})
assert result.key?(@name)
end
should "add an association with a factory name" do
factory = Factory.new(:post)
attr = 'attribute'
Factory::Attribute::Association.
expects(:new).
with(:author, :user, {}).
returns(attr)
factory.association(:author, :factory => :user)
assert factory.attributes.include?(attr)
end

should "create a block that builds the association" do
Factory.expects(:create).with(@factory_name, {})
@factory.run(Factory::Proxy::Build, {})
end
should "add an association with a factory name and overrides" do
factory = Factory.new(:post)
attr = 'attribute'
Factory::Attribute::Association.
expects(:new).
with(:author, :user, :first_name => 'Ben').
returns(attr)
factory.association(:author, :factory => :user, :first_name => 'Ben')
assert factory.attributes.include?(attr)
end

should "add an attribute using the method name when passed an undefined method" do
Expand Down

0 comments on commit 8e33e61

Please sign in to comment.