FactoryBot: FactoryBot::DuplicateDefinitionError: Factory already registered: user
Two factory definitions use the same name (or files are loaded twice).
FactoryBot::DuplicateDefinitionError: Factory already registered: userFactory already registered: userFactoryGirl::DuplicateDefinitionError: Factory already registeredFactory already registered: admin_userDuplicateDefinitionError Factory already registered Meaning
Search for factory :user across spec/factories and test/factories. Duplicates arise from two files, copy/paste, or loading factories twice (FactoryBot.find_definitions plus factory_bot_rails auto-load). Rename one or merge them; use traits for variations.
Common causes
- Same factory name defined in two files
- Factories loaded twice (find_definitions + factory_bot_rails)
- Gem engine providing the same factory
- Copy-pasted file
- Lint task loading twice
⚡ Quick fix
grep -rn "factory :user" spec testand remove the duplicate- Don't call
FactoryBot.find_definitionsmanually if using factory_bot_rails - Use traits instead of duplicate factories
Detailed fix by platform
Ruby
grep -rn "factory :user" spec/ test/
How to diagnose
- Duplicates — Which files?
- Loading — Twice?
- Traits — Better?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- ActiveRecord::RecordInvalid: Validation failed Rails: ActiveRecord::RecordInvalid: Validation failed: Email can't be blank, Name is too short
- ArgumentError: missing keyword / unknown keyword Ruby: ArgumentError: missing keyword: :name / unknown keyword: :nmae
- ArgumentError: wrong number of arguments (given 1, expected 2) Ruby: ArgumentError: wrong number of arguments (given 1, expected 2..3)
Most viewed in Ruby / Rails
- ActiveRecord::PendingMigrationError Rails: ActiveRecord::PendingMigrationError — Migrations are pending. To resolve this issue, run: bin/rails db:migrate
- undefined method for nil Ruby: NoMethodError: undefined method `x' for nil (nil:NilClass)
- ActionController::InvalidAuthenticityToken Rails: ActionController::InvalidAuthenticityToken (Can't verify CSRF token authenticity)
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 5 Nov 2026