Skip to content

Commit

Permalink
thoughtbot#25 - Factory.build now uses Factory.create for associations
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Nov 28, 2008
1 parent 153ecd0 commit 56a1732
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/factory_girl/attribute_proxy.rb
Expand Up @@ -47,7 +47,7 @@ def association (name, attributes = {})
if strategy == :attributes_for
nil
else
Factory.send(strategy, name, attributes)
Factory.create(name, attributes)
end
end

Expand Down
20 changes: 20 additions & 0 deletions test/attribute_proxy_test.rb
Expand Up @@ -68,6 +68,26 @@ class AttributeProxyTest < Test::Unit::TestCase

end

context "building an association using the build strategy" do

setup do
@strategy = :build
@built = 'object'
@proxy = Factory::AttributeProxy.new(@factory, @attr, @strategy, @attrs)
Factory.stubs(:create).returns(@built)
end

should "create the association" do
Factory.expects(:create).with(:user, {}).returns(@built)
@proxy.association(:user)
end

should "return the created object" do
assert_equal @built, @proxy.association(:user)
end

end

context "fetching the value of an attribute" do

setup do
Expand Down
6 changes: 4 additions & 2 deletions test/factory_test.rb
Expand Up @@ -159,14 +159,15 @@ def self.should_instantiate_class
@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
assert @factory.attributes_for.key?(@name)
end

should "create a block that builds the association" do
Factory.expects(:build).with(@name, {})
Factory.expects(:create).with(@name, {})
@factory.build
end

Expand All @@ -179,14 +180,15 @@ def self.should_instantiate_class
@name = :author
@factory_name = :user
@factory.association(@name, :factory => @factory_name)
Factory.stubs(:create)
end

should "add an attribute with the name of the association" do
assert @factory.attributes_for.key?(@name)
end

should "create a block that builds the association" do
Factory.expects(:build).with(@factory_name, {})
Factory.expects(:create).with(@factory_name, {})
@factory.build
end

Expand Down
4 changes: 2 additions & 2 deletions test/integration_test.rb
Expand Up @@ -70,8 +70,8 @@ def teardown
assert_kind_of User, @instance.author
end

should "not save associations" do
assert @instance.author.new_record?
should "save associations" do
assert !@instance.author.new_record?
end

should "not assign both an association and its foreign key" do
Expand Down

0 comments on commit 56a1732

Please sign in to comment.