Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

CodeMeister
Copy link
Contributor

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

- 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
# for free to join this conversation on GitHub. Already have an account? # 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
1 participant