Skip to content

Commit a4e5ab7

Browse files
authored
DefinitionProxy#method_missing: Forward given block to #association (#1579)
Fixes #1503 Why: ---- When defining an association the block-argument will be ignored when also providing a `factory`-key on the association definition. This behaviour might be unexpected, so we should raise an error that the passed block is unexpected/ignored.
1 parent d3d6b85 commit a4e5ab7

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

lib/factory_bot/definition_proxy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def method_missing(name, *args, &block) # rubocop:disable Style/MissingRespondTo
9494
if association_options.nil?
9595
__declare_attribute__(name, block)
9696
elsif __valid_association_options?(association_options)
97-
association(name, association_options)
97+
association(name, association_options, &block)
9898
else
9999
raise NoMethodError.new(<<~MSG)
100100
undefined method '#{name}' in '#{@definition.name}' factory

spec/factory_bot/definition_proxy_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@
7373
/'static_attributes_are_gone'.*'broken' factory.*Did you mean\? 'static_attributes_are_gone \{ "true" \}'/m
7474
)
7575
end
76+
77+
it "raises an AssociationDefinitionError when called with a `:factory`-key and providing a block" do
78+
definition = FactoryBot::Definition.new(:user)
79+
proxy = FactoryBot::DefinitionProxy.new(definition)
80+
invalid_call = lambda do
81+
proxy.author(factory: :user) { :this_should_raise_an_error }
82+
end
83+
84+
expect(invalid_call).to raise_error(
85+
FactoryBot::AssociationDefinitionError,
86+
"Unexpected block passed to 'author' association in 'user' factory"
87+
)
88+
end
7689
end
7790

7891
describe FactoryBot::DefinitionProxy, "#sequence" do

0 commit comments

Comments
 (0)