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

Build an association without arguments in a definition proxy

This commit is contained in:
Joe Ferris 2010-07-06 21:15:16 -04:00
parent bf0b6a6c16
commit c7bb677777
3 changed files with 33 additions and 7 deletions

View file

@ -26,7 +26,7 @@ module FactoryGirl
# generated instances.
# * value: +Object+
# If no block is given, this value will be used for this attribute.
def add_attribute (name, value = nil, &block)
def add_attribute(name, value = nil, &block)
if block_given?
if value
raise AttributeDefinitionError, "Both value and block given"
@ -54,8 +54,12 @@ module FactoryGirl
# end
#
# are equivilent.
def method_missing (name, *args, &block)
add_attribute(name, *args, &block)
def method_missing(name, *args, &block)
if args.empty? && block.nil?
association(name)
else
add_attribute(name, *args, &block)
end
end
# Adds an attribute that will have unique values generated by a sequence with

View file

@ -11,9 +11,13 @@ describe "integration" do
email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
end
# TODO: add sugar for this
factory :author, :parent => :user do
end
factory Post, :default_strategy => :attributes_for do
name 'Test Post'
association :author, :factory => :user
author
end
factory :admin, :class => User do
@ -21,6 +25,7 @@ describe "integration" do
last_name 'Stein'
admin true
sequence(:username) { |n| "username#{n}" }
# TODO: add sugar for this
email { Factory.next(:email) }
end

View file

@ -5,12 +5,12 @@ describe FactoryGirl::DefinitionProxy do
subject { FactoryGirl::DefinitionProxy.new(factory) }
it "should add a static attribute for type" do
subject.type
subject.type 'value'
factory.attributes.last.should be_kind_of(FactoryGirl::Attribute::Static)
end
it "should add a static attribute for id" do
subject.id
subject.id 'value'
factory.attributes.last.should be_kind_of(FactoryGirl::Attribute::Static)
end
@ -97,9 +97,26 @@ describe FactoryGirl::DefinitionProxy do
it "should add an attribute using the method name when passed an undefined method" do
attribute = 'attribute'
stub(attribute).name { :name }
block = lambda {}
mock(FactoryGirl::Attribute::Static).new(:name, 'value') { attribute }
subject.send(:name, 'value')
factory.attributes.should include(attribute)
end
it "adds an attribute using when passed an undefined method and block" do
attribute = 'attribute'
stub(attribute).name { :name }
block = lambda {}
mock(FactoryGirl::Attribute::Dynamic).new(:name, block) { attribute }
subject.send(:name, &block)
factory.attributes.should include(attribute)
end
it "adds an association when passed an undefined method without arguments or a block" do
name = :user
attr = 'attribute'
stub(attr).name { name }
mock(FactoryGirl::Attribute::Association).new(name, name, {}) { attr }
subject.send(name)
factory.attributes.should include(attr)
end
end