Browse files

#25 - Factory.build now uses Factory.create for associations

  • Loading branch information...
1 parent 153ecd0 commit 56a1732e90b4cccea62bd3dffbf62934892a4320 @jferris jferris committed Nov 28, 2008
Showing with 27 additions and 5 deletions.
  1. +1 −1 lib/factory_girl/attribute_proxy.rb
  2. +20 −0 test/attribute_proxy_test.rb
  3. +4 −2 test/factory_test.rb
  4. +2 −2 test/integration_test.rb
View
2 lib/factory_girl/attribute_proxy.rb
@@ -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
View
20 test/attribute_proxy_test.rb
@@ -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
View
6 test/factory_test.rb
@@ -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
@@ -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
View
4 test/integration_test.rb
@@ -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

0 comments on commit 56a1732

Please sign in to comment.