1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Fixed the attributes_for build strategy to not build associations

This commit is contained in:
Joe Ferris 2008-05-31 22:54:19 -07:00
parent f128e87121
commit 6ee6c0282b
3 changed files with 41 additions and 2 deletions

View file

@ -13,6 +13,18 @@ class Factory
# Generates an association using the current build strategy.
#
# Arguments:
# name: (Symbol)
# The name of the factory that should be used to generate this
# association.
# attributes: (Hash)
# A hash of attributes that should be overridden for this association.
#
# Returns:
# The generated association for the current build strategy. Note that
# assocaitions are not generated for the attributes_for strategy. Returns
# nil in this case.
#
# Example:
#
# Factory.define :user do |f|
@ -32,7 +44,11 @@ class Factory
# Factory.create(:post)
#
def association (name, attributes = {})
Factory.send(strategy, name, attributes)
if strategy == :attributes_for
nil
else
Factory.send(strategy, name, attributes)
end
end
# Returns the value for specified attribute. A value will only be available

View file

@ -49,6 +49,25 @@ class AttributeProxyTest < Test::Unit::TestCase
end
context "building an association using the attributes_for strategy" do
setup do
@strategy = :attributes_for
@proxy = Factory::AttributeProxy.new(@factory, @attr, @strategy, @attrs)
end
should "not build the association" do
Factory.expects(@strategy).never
@proxy.association(:user)
end
should "return nil for the association" do
Factory.stubs(@strategy).returns(:user)
assert_nil @proxy.association(:user)
end
end
context "fetching the value of an attribute" do
setup do

View file

@ -46,6 +46,10 @@ class IntegrationTest < Test::Unit::TestCase
assert_equal 'Bill', @attrs[:first_name]
end
should "not assign associations" do
assert_nil Factory.attributes_for(:post)[:author]
end
end
context "a built instance" do
@ -68,7 +72,7 @@ class IntegrationTest < Test::Unit::TestCase
end
context "a built instance" do
context "a created instance" do
setup do
@instance = Factory.create(:post)