`FactoryBot::DuplicateDefinitionError` on dev server startup
Context: My development server (./bin/dev
) failed to boot, reporting FactoryBot::DuplicateDefinitionError: Factory already registered: learning
.
Cause: The factory_bot_rails
gem was included in the group :development, :test do
block in the Gemfile
. This caused FactoryBot's Railtie to load factory definitions (spec/factories/*.rb
) during development environment initialization (e.g., via Spring or initial boot), potentially conflicting with other initializers or running twice.
Resolution: I moved gem "factory_bot_rails"
exclusively to the group :test do
block in the Gemfile
and ran rbenv exec bundle install
.
Learning: I learned that gems intended solely for testing (like factory_bot_rails
, shoulda-matchers
, capybara
) should typically only be included in the :test
group in the Gemfile
. Including them in :development
can lead to unexpected behavior, errors during server startup, or unnecessary loading as their initializers might run inappropriately or cause conflicts.
Learned on: April 15, 2024
Edit