Factory already registered: user (FactoryGirl::DuplicateDefinitionError)

给你一囗甜甜゛ 提交于 2019-12-03 04:11:28

The gem factory_girl_rails should be required in the spec_helper.rb rather than the gemfile - it is possible that you are requiring FactoryGirl twice which is why you are getting the duplicate.

Try this in your gem file:

group :test do
  gem "rspec"
  gem 'factory_girl_rails', :require => false
end

Then make sure that factory girl is required in the spec_helper with:

require 'factory_girl_rails'

By the way - you don't need both rspec and rpsec-rails in your gemfile. You can replace both with the following:

group :development, :test do
  gem 'rspec-rails'
end

You need rspec in both groups so that the rake tasks will work in development and the core testing will work in test.

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.

I had this problem too. In my case there were two files with the same code, like this:

FactoryGirl.define do
  factory :user do
  end
end

One file was named "Useres.rb" and the other "User.rb" so I just deleted "Useres.rb" and fixed the error.

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.

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  

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

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

https://github.com/thoughtbot/factory_girl/issues/638


Loading factory girl into a development console will do this too:

require 'factory_girl_rails'; reload!; FactoryGirl.factories.clear; FactoryGirl.find_definitions

will raise a FactoryGirl::DuplicateDefinitionError on a sequence under Factory Girl v4.4.0.

It seems the sequences get handled differently within FG and simply wrapping all sequences in a rescue block will solve the issue.

For example:

  begin
    sequence :a_sequence do |n|
      n
    end
    sequence :another_sequence do |n|
      n*2
    end
  rescue FactoryGirl::DuplicateDefinitionError => e
    warn "#{e.message}"
  end

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

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

I had the same problem- make sure you aren't loading FactoryGirl a second time in your spec/support/env.rb file.

I had same problem. This happens becouse of you using gem 'refinerycms-testing'? wich requires factory-girl, so you should commit this gem, or commit gem 'factory_girl_rails', don't use all of this gems.

    #gem 'refinerycms-testing', '~> 2.0.9', :group => :test
    gem 'factory_girl_rails', :group => :test

or

    #gem 'factory_girl_rails', :group => :test
    gem 'refinerycms-testing', '~> 2.0.9', :group => :test 
guest

Please try following these steps

1) I looked for all occurrences of "factory_girl" from my RAILS_ROOT:

find . -name "*.rb" | xargs grep "factory_girl"

2) Because this was a full engine plugin "app" that I created via "rails plugin new --mountable", I had a file under RAILS_ROOT//lib/ called "engine.rb". It had:

config.generators do |g|
  g.test_framework      :rspec,        :fixture => false
  g.fixture_replacement :factory_girl, :dir => 'spec/factories'
  g.assets false
  g.helper false
end

3) I also had the following in my spec_helper.rb file:

Dir["#{File.dirname(FILE)}/factories/*/.rb"].each { |f| require f }

4) the g.fixture_replacement line in engine.rb and the Dir line in spec_helper.rb were initializing the factories twice. I commented out the one from spec_helper.rb and that fixed the problem.

Alternatively, you can leave in spec_helper.rb and comment out in engine.rb.

Both fixed the problem in my case.

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

I had the same problem, it turned out there was a default users.rb created inside the test/factories which was created by the rails g command. This file was causing the conflict. The error went away when I deleted the file.

try to run

rake db:test:prepare

I just found I was getting this answer when accidentally calling cucumber features. When I just called cucumber, the problem went away.

cvibha

I also ran with the same issue and commenting out a single line in spec_helper.rb file solved my problem.

Try commenting out this line from spec_helper.rb file and you should be good.

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

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.

Replace the refinerycms-testing gem with rspec-rails and factory_girl_rails

Check to see if you added factories through the model generator. My generator made a model and I added one to my main factory.rb file. Deleting the automatically generated ones worked for me.

In my case,

First my co-worker has setup the project with factory_girl gem with

Dir[Rails.root.join('spec/factories/**/*.rb')].each { |f| require f }

in rails_helper.

After some days, I replaced the gem with factory_girl_rails. Since this new gem also does that internally so factories were registered twice. This was causing the error.

Removed that line from rails_helper and it worked.

I solved this because I was trying to create two factories. My feature spec included the line:

let!(:user) { create(:user) }

And then I used a sign_up(user) helper method:

def sign_up(user)
  visit '/users/sign_up'
  fill_in 'Email', with: user.email
  fill_in 'Password', with: user.password
  fill_in 'Password confirmation', with: user.password_confirmation
  click_button 'Sign up'
end

Back to my feature spec, I called:

context 'logging out' do
  before do
    sign_up(user)
  end

...

thus effectively trying to sign up a User that was already being created by the factory.

I altered the sign_up(user) to sign_in(user), and the helper to:

def sign_in(user)
  visit '/users/sign_in'
  fill_in 'Email', with: user.email
  fill_in 'Password', with: user.password
  click_button 'Log in'
end 

now the user argument creates the User in the db due to the let! block and the sign_up(user) logs them in.

Hope this helps someone!

oh! and I also had to comment out:

Dir[Rails.root.join('spec/factories/**/*.rb')].each { |f| require f }

as a lot of the other answers suggest.

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.

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

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.

Check that you don't have multiple factories with same name this is one of reasons which causes error

Attempting to define multiple factories with the same name will raise an error.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!