Ruby-on-rails 工厂已注册:用户(FactoryGirl::DuplicateDefinitionError)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9300231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Factory already registered: user (FactoryGirl::DuplicateDefinitionError)
提问by user1212241
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.
问题描述:- 我已经设置了 factory_girl_rails 但是每当我尝试加载工厂时,它都会尝试多次加载。
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
我唯一改变的另一件事是:/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:
编辑:这是我的规范/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
回答by nmott
The gem factory_girl_railsshould be required in the spec_helper.rbrather than the gemfile - it is possible that you are requiring FactoryGirl twice which is why you are getting the duplicate.
factory_girl_rails应该在gem 文件中spec_helper.rb而不是在 gemfile 中需要 gem - 您可能需要两次 FactoryGirl,这就是为什么您会得到重复的原因。
Try this in your gem file:
在你的 gem 文件中试试这个:
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:
然后确保在 spec_helper 中需要工厂女孩:
require 'factory_girl_rails'
By the way - you don't need both rspecand rpsec-railsin your gemfile. You can replace both with the following:
顺便说一句 - 您不需要在您的 gemfile 中同时使用rspec和rpsec-rails。您可以将两者替换为以下内容:
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.
您在两个组中都需要 rspec,以便 rake 任务将在开发中工作,核心测试将在测试中工作。
回答by yellowKompressor
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.
我最近遇到了同样的问题。在我的例子中, /factories 中的一个文件有一个 _spec.rb 结尾(创造性 cp 使用的结果)。它加载了两次,首先是通过 rspec 加载,然后是作为工厂加载。
回答by Eduardo Leal
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.
一个文件名为“Useres.rb”,另一个名为“User.rb”,所以我删除了“Useres.rb”并修复了错误。
回答by Ryan Bosinger
Is there any chance you pasted this wholesnippet 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.
如果您阅读评论,您会发现您只想要一个或另一个。我犯了这个错误并得到了问题中所述的错误。
回答by Moriarty
Call FactoryGirl.define(:user)or FactoryGirl.find_definitionstwice you also have this problem.
打电话FactoryGirl.define(:user)或FactoryGirl.find_definitions两次你也有这个问题。
Try removing the second callor:
尝试删除第二个电话或:
FactoryGirl.factories.clear
FactoryGirl.find_definitions
回答by Denis Kreshikhin
Another possible reason is spare call of FactoryGirl.find_definitions.
Try to remove find_definitions if found.
另一个可能的原因是FactoryGirl.find_definitions. 如果找到,请尝试删除 find_definitions。
回答by scarver2
Make sure your individual factory files are not ending with _spec.
确保您的个人工厂文件不以_spec.
回答by bbozo
https://github.com/thoughtbot/factory_girl/issues/638
https://github.com/thoughtbot/factory_girl/issues/638
Loading factory girl into a development console will do this too:
将 factory girl 加载到开发控制台也将执行此操作:
require 'factory_girl_rails'; reload!; FactoryGirl.factories.clear; FactoryGirl.find_definitions
will raise a FactoryGirl::DuplicateDefinitionErroron a sequence under Factory Girl v4.4.0.
将FactoryGirl::DuplicateDefinitionError在 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.
似乎序列在 FG 中的处理方式不同,只需将所有序列包装在救援块中即可解决问题。
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
回答by user1458963
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
我遇到了完全相同的问题。当您使用脚手架生成器时会发生这种情况。它会自动在 test/factories/ 中创建一个工厂,因此通常只需删除此文件即可解决您的问题
回答by ultrajohn
I had the same problem, it turned out there was a default users.rbcreated inside the test/factorieswhich was created by the rails gcommand. This file was causing the conflict. The error went away when I deleted the file.
我遇到了同样的问题,原来users.rb在里面test/factories创建了一个默认值,它是由rails g命令创建的。该文件导致了冲突。当我删除文件时,错误消失了。

