Skip to content

Commit

Permalink
Simplify association syntax when defining new factory.
Browse files Browse the repository at this point in the history
Now it's possible to do:

factory :post do
  author :factory => :user
end
  • Loading branch information
szimek committed Jul 30, 2011
1 parent bfe1271 commit d4edf6f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/factory_girl/definition_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def add_attribute(name, value = nil, &block)
def method_missing(name, *args, &block)
if args.empty? && block.nil?
@factory.define_attribute(Attribute::Implicit.new(name))
elsif args.first.is_a?(Hash) && args.first.has_key?(:factory)
association(name, *args)
else
add_attribute(name, *args, &block)
end
Expand Down
12 changes: 12 additions & 0 deletions spec/factory_girl/definition_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@
factory.attributes.should include(attr)
end

it "adds an association when passed an undefined method with a hash including :factory key" do
name = :author
factory_name = :user
overrides = { :first_name => 'Ben' }
args = { :factory => factory_name }.merge(overrides)
attr = 'attribute'
stub(attr).name { name }
mock(FactoryGirl::Attribute::Association).new(name, factory_name, overrides) { attr }
subject.send(name, args)
factory.attributes.should include(attr)
end

it "delegates to_create" do
result = 'expected'
mock(factory).to_create { result }
Expand Down

0 comments on commit d4edf6f

Please sign in to comment.