Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 724
user1212241 Avatar asked Feb 15 '12 19:02

user1212241


1 Answers

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.

like image 105
nmott Avatar answered Oct 04 '22 16:10

nmott