mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
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}' ```
This commit is contained in:
parent
218eb72984
commit
f606018a78
2 changed files with 30 additions and 3 deletions
|
@ -25,7 +25,7 @@ module FactoryBot
|
||||||
attr_reader :factory_name, :overrides, :traits
|
attr_reader :factory_name, :overrides, :traits
|
||||||
|
|
||||||
def build
|
def build
|
||||||
ensure_factory_is_not_a_declaration!
|
raise_if_arguments_are_declarations!
|
||||||
|
|
||||||
[
|
[
|
||||||
Attribute::Association.new(
|
Attribute::Association.new(
|
||||||
|
@ -36,13 +36,22 @@ module FactoryBot
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
def ensure_factory_is_not_a_declaration!
|
def raise_if_arguments_are_declarations!
|
||||||
if factory_name.is_a?(Declaration)
|
if factory_name.is_a?(Declaration)
|
||||||
raise ArgumentError.new(<<~MSG)
|
raise ArgumentError.new(<<~MSG)
|
||||||
Association '#{name}' received an invalid factory argument.
|
Association '#{name}' received an invalid factory argument.
|
||||||
Did you mean? 'factory: :#{factory_name.name}'
|
Did you mean? 'factory: :#{factory_name.name}'
|
||||||
MSG
|
MSG
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
describe "associations" do
|
describe "associations" do
|
||||||
context "when accidentally using an implicit delcaration for the factory" 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")
|
define_class("Post")
|
||||||
|
|
||||||
FactoryBot.define do
|
FactoryBot.define do
|
||||||
|
@ -16,4 +16,22 @@ describe "associations" do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue