Skip to content

Expanded trait error message to include list of defined traits. - #1824

Open
iannorriswork wants to merge 7 commits into
thoughtbot:mainfrom
iannorriswork:main
Open

Expanded trait error message to include list of defined traits.#1824
iannorriswork wants to merge 7 commits into
thoughtbot:mainfrom
iannorriswork:main

Conversation

@iannorriswork

@iannorriswork iannorriswork commented Jul 15, 2026

Copy link
Copy Markdown

Fixes: #1727, building off of #1732, but preserving DidYouMean behavior in traits as well.

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" definition

User 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" definition

Changes
lib/factory_bot/definition.rb

error-rescue moved from :base_traits to :trait_by_name to catch both definition and calling errors.
spec/acceptance/enum_traits_spec.rb

updated specs for new error message layout.
spec/acceptance/traits_spec.rb updated specs for new error message layout. added new use-case tests with:

no registered traits
single registered trait
multiple registered traits
multiple registered traits through multiple inheritance
trait DidYouMean tests

@vburzynski vburzynski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some initial thoughts 😎. So far this looks great overall.

Comment thread lib/factory_bot/definition.rb Outdated
Comment thread lib/factory_bot/definition.rb Outdated

@vburzynski vburzynski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good on a scan through. 😎

I think I'd like to allow @neilvcarvalho a chance to review (he opened the related issues) and review the specs with some more focus review the specs closer after the Hack Space.

@vburzynski

Copy link
Copy Markdown
Contributor

Just wanted to pop in and say I still have this on my radar to review. Things have been a bit busy after getting back from rubyconf 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show the registered traits in the "Trait not registered" error message

2 participants