Factory already registered: user (FactoryGirl::DuplicateDefinitionError)

后端 未结 27 2185
我在风中等你
我在风中等你 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:47

    I resolved it by removing spec/factories/xxx.rb from command line:

    rspec spec/factories/xxx.rb spec/model/xxx.rb # before
    rspec spec/model/xxx.rb # after
    
    0 讨论(0)
  • 2021-02-06 21:50

    I have the same the problem. What I do is move the spec/factories.rb to spec/factories/role.rb

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

    I defined the same name factory at factories.rb, and I just found that someone else define the same factory below the directory of factories. So actually I can just use it without define another one.

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

    I had the same problem recently. In my case one of the files in /factories had a _spec.rb ending (result of creative cp use). It was loading twice, first by rspec and then as a factory.

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

    I renamed spec/factories as spec/setup_data and the problem gone. Try renaming the spec/factories to anything that suites you, should work.

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

    Is there any chance you pasted this whole snippet for the support file from the config docs?

    # RSpec
    # spec/support/factory_girl.rb
    RSpec.configure do |config|
      config.include FactoryGirl::Syntax::Methods
    end
    
    # RSpec without Rails
    RSpec.configure do |config|
      config.include FactoryGirl::Syntax::Methods
    
      config.before(:suite) do
        FactoryGirl.find_definitions
      end
    end
    

    If you read the comments you'll see you only want one block or the other. I made this mistake and got the error stated in the question.

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