Factory already registered: user (FactoryGirl::DuplicateDefinitionError)

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.

Environment:
- rails (3.2.1)
- factory_girl (2.5.2)
- factory_girl_rails (1.6.0)
- ruby-1.9.3-p0 [ x86_64 ]

> rake spec --trace
** Execute environment
-- Creating User Factory
-- Creating User Factory
rake aborted!
Factory already registered: user

The only other thing I've changed is: /config/initializers/generator.rb

Rails.application.config.generators do |g|
  g.test_framework = :rspec
  g.fixture_replacement :factory_girl
end

GEMFILE

gem 'rails', '3.2.1'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'
gem 'devise'
gem 'haml-rails'

group :development do
  gem 'hpricot'
  gem 'ruby_parser'
  gem "rspec-rails"
end

group :test do
  gem "rspec"
  gem 'factory_girl_rails'
end

gem 'refinerycms-core',       :git => 'git://github.com/resolve/refinerycms.git'
gem 'refinerycms-dashboard',  :git => 'git://github.com/resolve/refinerycms.git'
gem 'refinerycms-images',     :git => 'git://github.com/resolve/refinerycms.git'
gem 'refinerycms-pages',      :git => 'git://github.com/resolve/refinerycms.git'
gem 'refinerycms-resources',  :git => 'git://github.com/resolve/refinerycms.git'
gem 'refinerycms-settings',   :git => 'git://github.com/resolve/refinerycms.git'

group :development, :test do
  gem 'refinerycms-testing',  :git => 'git://github.com/resolve/refinerycms.git'
end

gem 'refinerycms-inventories', :path => 'vendor/engines'

FactoryGirl.define do
  factory :role do
    title "MyString"
  end
end

This seems to be a compatibility/environment issue that I can't seem to figure out. Any suggestions?

EDIT: here's my spec/spec_helper.rb:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
#require 'factory_girl_rails'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
### Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  #config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false
end

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.


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.


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.