diff --git a/lib/factory_girl/definition_proxy.rb b/lib/factory_girl/definition_proxy.rb index 41c6a8008..b9be6bcbb 100644 --- a/lib/factory_girl/definition_proxy.rb +++ b/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 diff --git a/spec/factory_girl/definition_proxy_spec.rb b/spec/factory_girl/definition_proxy_spec.rb index 1a0cb4c6a..2b85d8243 100644 --- a/spec/factory_girl/definition_proxy_spec.rb +++ b/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 }