Factory_bot: Factory already registered, beginner question

Created on 27 Apr 2018  路  6Comments  路  Source: thoughtbot/factory_bot

Hi factory_bot community
First of all, thanks for putting all the work into factory_bot!

I am new to factory_bot, and can't understand why I get the DuplicateDefinitionError with the following factory file:

FactoryBot.define do
  sequence :email do |n|
    "person#{n}@example.com"
  end

  sequence :name do |n|
    "Name #{n}"
  end

  factory :employee do
    email
    company
    name
  end

  factory :project do
    company
    name
  end

  factory :company do
    name "Test Company"

    factory :full_company do
      transient do
        employees_count 5
        projects_count 2
      end

      after(:create) do |company, evaluator|
        create_list(:employee, evaluator.employees_count, company: company)
        create_list(:project, evaluator.projects_count, company: company)
      end
    end
  end
end

Can someone point me in the direction of what I am doing wrong?

Thanks!

Most helpful comment

@stephenson looks like test/factories/projects.rb is causing problems; any chance you could verify?

All 6 comments

@stephenson hmm, there's nothing obvious here that seems out of place; are there multiple factory files (e.g. spec/factories.rb and spec/factories/employee.rb) where there might be overlap? Could you provide the backtrace from the exception in hopes that it leads to further clues?

@joshuaclayton Only one factory file in the project, this is the full output in the terminal (is that the backtrace? Sorry noob here):

/Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot-4.8.2/lib/factory_bot/decorator.rb:10:in `method_missing': Factory already registered: project (FactoryBot::DuplicateDefinitionError)
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot-4.8.2/lib/factory_bot/decorator/disallows_duplicates_registry.rb:6:in `register'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot-4.8.2/lib/factory_bot.rb:94:in `block in register_factory'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot-4.8.2/lib/factory_bot.rb:93:in `each'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot-4.8.2/lib/factory_bot.rb:93:in `register_factory'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot-4.8.2/lib/factory_bot/syntax/default.rb:20:in `factory'
    from /Users/nst/work/termstrail/test/factories/projects.rb:2:in `block in <top (required)>'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot-4.8.2/lib/factory_bot/syntax/default.rb:49:in `instance_eval'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot-4.8.2/lib/factory_bot/syntax/default.rb:49:in `run'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot-4.8.2/lib/factory_bot/syntax/default.rb:7:in `define'
    from /Users/nst/work/termstrail/test/factories/projects.rb:1:in `<top (required)>'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `block in load'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:286:in `load'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot-4.8.2/lib/factory_bot/find_definitions.rb:20:in `block (2 levels) in find_definitions'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot-4.8.2/lib/factory_bot/find_definitions.rb:19:in `each'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot-4.8.2/lib/factory_bot/find_definitions.rb:19:in `block in find_definitions'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot-4.8.2/lib/factory_bot/find_definitions.rb:15:in `each'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot-4.8.2/lib/factory_bot/find_definitions.rb:15:in `find_definitions'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot-4.8.2/lib/factory_bot/reload.rb:6:in `reload'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/factory_bot_rails-4.8.2/lib/factory_bot_rails/railtie.rb:24:in `block (2 levels) in <class:Railtie>'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/spring-2.0.2/lib/spring/application.rb:263:in `block in invoke_after_fork_callbacks'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/spring-2.0.2/lib/spring/application.rb:262:in `each'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/spring-2.0.2/lib/spring/application.rb:262:in `invoke_after_fork_callbacks'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/spring-2.0.2/lib/spring/application.rb:198:in `block in serve'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/spring-2.0.2/lib/spring/application.rb:171:in `fork'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/spring-2.0.2/lib/spring/application.rb:171:in `serve'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/spring-2.0.2/lib/spring/application.rb:141:in `block in run'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/spring-2.0.2/lib/spring/application.rb:135:in `loop'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/spring-2.0.2/lib/spring/application.rb:135:in `run'
    from /Users/nst/.rvm/gems/ruby-2.4.2@termstrail/gems/spring-2.0.2/lib/spring/application/boot.rb:19:in `<top (required)>'
    from /Users/nst/.rvm/rubies/ruby-2.4.2/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/nst/.rvm/rubies/ruby-2.4.2/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

@stephenson looks like test/factories/projects.rb is causing problems; any chance you could verify?

@joshuaclayton you are completely right, I did not see that the rails generator created a factory file. Still learning!

Thanks a lot for your time!

Thanks!

@stephenson looks like test/factories/projects.rb is causing problems; any chance you could verify?

Thanks, man! Hahaha

Was this page helpful?
0 / 5 - 0 ratings