Factory already registered: user (FactoryGirl::DuplicateDefinitionError)

后端 未结 27 2186
我在风中等你
我在风中等你 2021-02-06 21:18

Description of problem: - I\'ve setup factory_girl_rails however whenever I try and load a factory it\'s trying to load it multiple times.

Envir         


        
相关标签:
27条回答
  • 2021-02-06 21:41

    Make sure your individual factory files are not ending with _spec.

    0 讨论(0)
  • 2021-02-06 21:42

    Call FactoryGirl.define(:user) or FactoryGirl.find_definitions twice you also have this problem.

    Try removing the second call or:

    FactoryGirl.factories.clear
    FactoryGirl.find_definitions  
    
    0 讨论(0)
  • 2021-02-06 21:42

    The strangest thing, I got this error with the following syntax error in the code:

    before_validation :generate_reference, :on: :create
    

    :on: was causing this error. How or why will remain a mystery.

    0 讨论(0)
  • 2021-02-06 21:42

    for me, this issue was coming because was using both gems

    gem 'factory_bot_rails'
    gem 'factory_girl_rails'
    

    to solve I removed gem 'factory_bot_rails' from gem file. and also added require 'factory_girl' to spec/factories/track.rb file.

    if Rails.env.test?
        require 'factory_girl'
        FactoryGirl.define do
          factory :track do
                id 1
                name "nurburgring"
                surface_type "snow"
                time_zone "CET"
           end
        end
    

    I hope this will help.

    0 讨论(0)
  • 2021-02-06 21:46

    Another possible reason is spare call of FactoryGirl.find_definitions. Try to remove find_definitions if found.

    0 讨论(0)
  • 2021-02-06 21:46

    I had exactly the same problem. It occurs when you use the scaffold generator. It automatically creates a factory in test/factories/ So generally just deleting this file solve your issue

    0 讨论(0)
提交回复
热议问题