thoughtbot / thoughtbot/factory_bot

Don't allow duplicate traits

Open
#1,488 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Ruby
Stars
8.2k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Description

Description

A factory can have the same trait multiple times

Reproduction Steps
FactoryBot.define do
  factory :user, class: OpenStruct do
    trait :admin do
       name { "Admin" }
    end

    trait :admin do
       name { "Administrator" }
    end
  end
end
Expected behavior

Raises an error when defining the second trait

Actual behavior

Lets me define a second trait but keeps only the first trait

System configuration

factory_bot version: 6.1.0
rails version: 6.1.3.1
ruby version: 2.7.3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how a factory registers a trait during the trait definition entry point, then inspect the existing tests around trait definitions. Add a regression test for defining the same trait name twice and verify that the second definition raises an error rather than being silently ignored.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
testing
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.