FactoryBot::DuplicateDefinitionError: Factory already registered Ruby / Rails Origin: Application & API Last updated 5 Nov 2026

FactoryBot: FactoryBot::DuplicateDefinitionError: Factory already registered: user

Two factory definitions use the same name (or files are loaded twice).

Exact message you may see FactoryBot::DuplicateDefinitionError: Factory already registered: userFactory already registered: userFactoryGirl::DuplicateDefinitionError: Factory already registeredFactory already registered: admin_userDuplicateDefinitionError Factory already registered
Seen on: Ruby

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

  1. grep -rn "factory :user" spec test and remove the duplicate
  2. Don't call FactoryBot.find_definitions manually if using factory_bot_rails
  3. Use traits instead of duplicate factories

Detailed fix by platform

Ruby

  1. grep -rn "factory :user" spec/ test/

How to diagnose

  1. Duplicates — Which files?
  2. Loading — Twice?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

ActiveRecord::RecordInvalid: Validation failed — Rails: ActiveRecord::RecordInvalid: Validation failed: Email can't be blank, Name is too short (Ruby / Rails)ActiveRecord::RecordI…ArgumentError: missing keyword / unknown keyword — Ruby: ArgumentError: missing keyword: :name / unknown keyword: :nmae (Ruby / Rails)ArgumentError: missin…ArgumentError: wrong number of arguments (given 1, expected 2) — Ruby: ArgumentError: wrong number of arguments (given 1, expected 2..3) (Ruby / Rails)ArgumentError: wrong …Bundler could not find compatible versions for gem — Bundler: Bundler could not find compatible versions for gem "activesupport": In Gemfile: rails (= 7.1.0) depends on activesupport (= 7.1.0) (Ruby / Rails)Bundler could not fin…Bundler: Could not find gem in locally installed gems — Bundler: Could not find gem 'rails (~> 7.1)' in locally installed gems / Could not find rails-7.1.0 in any of the gem sources listed in your Gemfile (Ruby / Rails)Bundler: Could not fi…FrozenError: can't modify frozen String — Ruby: FrozenError: can't modify frozen String: "abc" (Ruby / Rails)FrozenError: can't mo…rake aborted! Don't know how to build task — Rake: rake aborted! Don't know how to build task 'db:migrat' (See the list of available tasks with `rake --tasks`) (Ruby / Rails)rake aborted! Don't k…RSpec::Expectations::ExpectationNotMetError: expected X got Y — RSpec: expected: 200 got: 404 (compared using ==) — RSpec::Expectations::ExpectationNotMetError (Ruby / Rails)RSpec::Expectations::…FactoryBo…

Explore the map of this error Full error map →

🧠 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.