[ACCEPTED]-Factory already registered: user (FactoryGirl::DuplicateDefinitionError)-ruby-on-rails-3.2

Accepted answer
Score: 38

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

Try this 7 in your gem file:

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

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

require 'factory_girl_rails'

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

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

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

Score: 23

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

Score: 16

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

# RSpec
# spec/support/factory_girl.rb
RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods

# RSpec without Rails
RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods

  config.before(:suite) do

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

Score: 15

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

FactoryGirl.define do
  factory :user do

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

Score: 11

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

Try removing the second call or:



Score: 9

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

Score: 6

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

Score: 2


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

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

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

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

For 1 example:

    sequence :a_sequence do |n|
    sequence :another_sequence do |n|
  rescue FactoryGirl::DuplicateDefinitionError => e
    warn "#{e.message}"
Score: 1

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

Score: 1

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

Score: 1

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

Score: 1

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

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


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

Please try following these steps

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

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

2) Because this was a 12 full engine plugin "app" that I created 11 via "rails plugin new --mountable", I had 10 a file under RAILS_ROOT//lib/ called "engine.rb". It 9 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

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

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

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

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

Both 1 fixed the problem in my case.

Score: 1

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

Score: 1

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

Score: 1

try to run

rake db:test:prepare


Score: 0

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

Score: 0

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

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

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

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

Score: 0

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

Score: 0

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

Score: 0

In my case,

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

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

in rails_helper.

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

Removed that line from rails_helper and it 1 worked.

Score: 0

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

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

And then I used a sign_up(user) helper 9 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'

Back to my feature spec, I called:

context 'logging out' do
  before do


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

I 6 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'

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

Hope 3 this helps someone!

oh! and I also had to 2 comment out:

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

as a lot of the other answers 1 suggest.

Score: 0

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

before_validation :generate_reference, :on: :create

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

Score: 0

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

rspec spec/factories/xxx.rb spec/model/xxx.rb # before
rspec spec/model/xxx.rb # after
Score: 0

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

gem 'factory_bot_rails'
gem 'factory_girl_rails'

to solve I removed gem 'factory_bot_rails' from 2 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"

I hope 1 this will help.

More Related questions