Expanded trait error message to include list of defined traits.#1732
Expanded trait error message to include list of defined traits.#1732CodeMeister wants to merge 1 commit intothoughtbot:mainfrom
Conversation
- Files changed:
- lib/factory_bot/definition.rb
- spec/acceptance/traits_spec.rb
- spec/acceptance/enum_traits_spec.rb
- Tests added:
- added new use-case tests for:
- no registered traits
- single registered trait
- multiple registered traits
- multiple registered traits through multiple inheritance
|
Hey @CodeMeister, I owe you a follow-up: while reviewing this PR and trying to understand how Factory Bot works with DidYouMean (and also how RSpec and Minitest work with it), and noticed that we re-raise the "Did you mean?" messages for factory definitions, but not for traits. I wonder if ultimately showing all the traits is the best solution (as there's DidYouMean). At the same time, I can only see the Did you mean? message when running Factory Bot in an irb session, not on RSpec or Minitest. Now I got into this rabbit hole 😄 |
|
Hi @neilvcarvalho, it's a conundrum for sure! DidYouMean is great when it's a close match, but not if it's completely wrong. I think it's good feedback to list all the available traits (including inherited ones), but DidYouMean would keep it consistent with factory definitions. How about we park this for now and I'll re-do it using DidYouMean? |
|
Sounds good! In the meantime, I'll check with the RSpec and MiniTest folks how to show the error's detailed message on Ruby >= 3.2. Since this version introduced the |
|
For reference, I opened the following issues: |
Summary
Fixes: #1727
Expands trait error messages to include a list of the factory's defined traits.
Included
The message now includes:
Registered traits: [:trait_1, :trait_2, :trait_3]Example
Internal Definition Call
factory: :user
defined traits: :accessible_trait
error: internal reference to a missing trait :inaccessible_trait
Error Message:
Trait not registered: "inaccessible_trait". Registered traits: [:accessible_trait]. Referenced within "user" definitionUser Trait Specified
factory: :user
defined traits: :accessible_trait
error: user calls an non-existent trait with
build(:user, :missing_trait)Error Message:
Trait not registered: "missing_trait". Registered traits: [:accessible_trait]Note
When the error is because a missing trait was called from within a factory's definition, the message still includes the original:
Referenced within "user" definitionChanges
lib/factory_bot/definition.rb
spec/acceptance/enum_traits_spec.rb
spec/acceptance/traits_spec.rb updated specs for new error message layout. added new use-case tests with: