thoughtbot / factory_bot_rails

Factory Bot ♥ Rails
https://thoughtbot.com/services/ruby-on-rails
MIT License
3.07k stars 369 forks source link

Duplicate factories problem #399

Closed mathieujobin closed 2 years ago

mathieujobin commented 3 years ago

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

composerinteralia commented 3 years ago

What happens in spec/spec_helper.rb:13? Any chance you are calling FactoryBot.find_definitions somewhere?

mathieujobin commented 3 years ago

@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

mathieujobin commented 3 years ago

Oh, line 13 is require 'factory_bot_rails'

composerinteralia commented 3 years ago

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?

composerinteralia commented 2 years ago

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.