本文介绍了工厂已注册:用户(FactoryGirl :: DuplicateDefinitionError)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题描述:-我已经设置了factory_girl_rails,但是无论何时尝试加载工厂,它都会尝试多次加载.

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

我唯一改变的另一件事是:/config/initializers/generator.rb

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?

编辑:这是我的spec/spec_helper.rb:

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

推荐答案

spec_helper.rb中应该使用gem factory_girl_rails而不是gemfile-可能需要两次FactoryFactory,这就是为什么要这样做副本.

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.

在您的gem文件中尝试一下:

Try this in your gem file:

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

然后使用以下命令确保在spec_helper中需要工厂女孩:

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

require 'factory_girl_rails'

顺便说一句-您的gemfile中不需要rspecrpsec-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

两个组中都需要rspec,以便rake任务可以在开发中工作,而核心测试也可以在测试中工作.

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

这篇关于工厂已注册:用户(FactoryGirl :: DuplicateDefinitionError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 03:18