diff --git a/lib/factory_bot/declaration/association.rb b/lib/factory_bot/declaration/association.rb index 9300cf4..123e73b 100644 --- a/lib/factory_bot/declaration/association.rb +++ b/lib/factory_bot/declaration/association.rb @@ -25,7 +25,7 @@ module FactoryBot attr_reader :factory_name, :overrides, :traits def build - ensure_factory_is_not_a_declaration! + raise_if_arguments_are_declarations! [ Attribute::Association.new( @@ -36,13 +36,22 @@ module FactoryBot ] end - def ensure_factory_is_not_a_declaration! + def raise_if_arguments_are_declarations! if factory_name.is_a?(Declaration) raise ArgumentError.new(<<~MSG) Association '#{name}' received an invalid factory argument. Did you mean? 'factory: :#{factory_name.name}' MSG end + + overrides.each do |attribute, value| + if value.is_a?(Declaration) + raise ArgumentError.new(<<~MSG) + Association '#{name}' received an invalid attribute override. + Did you mean? '#{attribute}: :#{value.name}' + MSG + end + end end end end diff --git a/spec/acceptance/associations_spec.rb b/spec/acceptance/associations_spec.rb index ed660fa..4b90dc6 100644 --- a/spec/acceptance/associations_spec.rb +++ b/spec/acceptance/associations_spec.rb @@ -1,6 +1,6 @@ describe "associations" do context "when accidentally using an implicit delcaration for the factory" do - it "raises an error about the trait not being registered" do + it "raises an error" do define_class("Post") FactoryBot.define do @@ -16,4 +16,22 @@ describe "associations" do ) end end + + context "when accidentally using an implicit delcaration as an override" do + it "raises an error" do + define_class("Post") + + FactoryBot.define do + factory :post do + author factory: :user, invalid_attribute: implicit_trait + end + end + + expect { FactoryBot.build(:post) }.to raise_error( + ArgumentError, + "Association 'author' received an invalid attribute override.\n" \ + "Did you mean? 'invalid_attribute: :implicit_trait'\n" + ) + end + end end