Duplicate factories problem
mathieujobin opened this issue · comments
Description
I am upgrading a rails app from 5.2 to 6.1
and I am getting a DuplicateDefinitionError.
Failure/Error:
factory :account_template do
end
FactoryBot::DuplicateDefinitionError:
Factory already registered: account_template
# ./spec/factories/account_templates.rb:4:in `block in <top (required)>'
# ./spec/factories/account_templates.rb:3:in `<top (required)>'
# ./spec/spec_helper.rb:13:in `<top (required)>'
Reproduction Steps
My Gemfile is like this
gem "factory_bot_rails", require: false
then in my spec_helper.rb
I do `require "factory_bot_rails"
I ran through using a debugger placed in the railtie of this gem.
it does not appear to be loaded twice.
but then I run this
(byebug) FactoryBot.factories.count
63
so something has ran them already?
I can't locate what. anyone else had this issue?
Expected behavior
My test should run like before
Actual behavior
fails on duplicate factory
System configuration
factory_bot_rails version: 6.2.0
factory_bot version: 6.2.0
rails version: 6.1.4.1
ruby version: 2.7.4
What happens in spec/spec_helper.rb:13
? Any chance you are calling FactoryBot.find_definitions
somewhere?
@composerinteralia
No, but I require 'spec_helper' in top of every test files, and it appears this block
config.after_initialize do |app|
FactoryBot.find_definitions
Reloader.new(app).run
end
runs in between every tests
Oh, line 13 is require 'factory_bot_rails'
It's surprising to me that the after_initialize
block is running more than once. That would indicate your are reinitializing rails in between every test, which shouldn't be necessary.
What do you see if you open your installed version of factory_bot_rails (for example by running bundle open factory_bot_rails
) and add something like puts caller
inside that block, before FactoryBot.find_definitions
?
Going to close this, since there's been no activity for a while. But happy to reopen if we get more information and can confirm a bug in factory_bot_rails.