Browse files

Simplify association syntax when defining new factory.

Now it's possible to do:

factory :post do
  author :factory => :user
end
  • Loading branch information...
1 parent bfe1271 commit d4edf6fdb7ea4042910aad70c819ac2270f3894b @szimek szimek committed Nov 28, 2010
Showing with 14 additions and 0 deletions.
  1. +2 −0 lib/factory_girl/definition_proxy.rb
  2. +12 −0 spec/factory_girl/definition_proxy_spec.rb
View
2 lib/factory_girl/definition_proxy.rb
@@ -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
View
12 spec/factory_girl/definition_proxy_spec.rb
@@ -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 }

0 comments on commit d4edf6f

Please sign in to comment.