mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
#11 - added syntactic sugar for associations
This commit is contained in:
parent
ab0b57a93d
commit
b4a5ab0eb8
3 changed files with 70 additions and 1 deletions
|
@ -123,6 +123,35 @@ class Factory
|
|||
add_attribute(name, *args, &block)
|
||||
end
|
||||
|
||||
# Adds an attribute that builds an association. The associated instance will
|
||||
# be built using the same build strategy as the parent instance.
|
||||
#
|
||||
# Example:
|
||||
# Factory.define :user do |f|
|
||||
# f.name 'Joey'
|
||||
# end
|
||||
#
|
||||
# Factory.define :post do |f|
|
||||
# f.association :author, :factory => :user
|
||||
# end
|
||||
#
|
||||
# Arguments:
|
||||
# name: (Symbol)
|
||||
# The name of this attribute.
|
||||
# options: (Hash)
|
||||
# factory: (Symbol)
|
||||
# The name of the factory to use when building the associated instance.
|
||||
# If no name is given, the name of the attribute is assumed to be the
|
||||
# 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 = options.symbolize_keys
|
||||
association_factory = options[:factory] || name
|
||||
|
||||
add_attribute(name) {|a| a.association(association_factory) }
|
||||
end
|
||||
|
||||
def attributes_for (attrs = {}) #:nodoc:
|
||||
build_attributes_hash(attrs, :attributes_for)
|
||||
end
|
||||
|
|
|
@ -168,6 +168,46 @@ class FactoryTest < Test::Unit::TestCase
|
|||
|
||||
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=)
|
||||
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.build
|
||||
end
|
||||
|
||||
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)
|
||||
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.build
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
should "add an attribute using the method name when passed an undefined method" do
|
||||
@attr = :first_name
|
||||
@value = 'Sugar'
|
||||
|
|
|
@ -12,7 +12,7 @@ class IntegrationTest < Test::Unit::TestCase
|
|||
|
||||
Factory.define 'post' do |f|
|
||||
f.name 'Test Post'
|
||||
f.author {|a| a.association(:user) }
|
||||
f.association :author, :factory => :user
|
||||
end
|
||||
|
||||
Factory.define :admin, :class => User do |f|
|
||||
|
|
Loading…
Reference in a new issue