From f606018a780556599212443dbfa43bfb50c91723 Mon Sep 17 00:00:00 2001 From: Daniel Colson Date: Sun, 21 Jun 2020 21:58:44 -0400 Subject: [PATCH] Raise more helpful error for invalid attribute Closes #1391 Along the same lines as #1286, this commit raises a more helpful error if somebody passes an implicit declaration as an association argument. Before this commit, an association with an implicit trait passed as an override attribute: ```rb person factory: :user, invalid_attribute: implicit_trait ``` Would raise an error `KeyError: Trait not registered: "implicit_trait"`. This is potentially confusing, since the author probably didn't intend to define an implicit trait. After this commit, this will raise a more helpful error: ``` ArgumentError: Association 'person' received an invalid attribute override. Did you mean? 'invalid_attribute}: :implicit_trait}' ``` --- lib/factory_bot/declaration/association.rb | 13 +++++++++++-- spec/acceptance/associations_spec.rb | 20 +++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) 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