工厂已经注册:user(FactoryGirl :: DuplicateDefinitionError)

use*_*241 44 ruby-on-rails ruby-on-rails-3.2 factory-bot

问题描述: - 我设置了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
Run Code Online (Sandbox Code Playgroud)

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

Rails.application.config.generators do |g|
  g.test_framework = :rspec
  g.fixture_replacement :factory_girl
end
Run Code Online (Sandbox Code Playgroud)

的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
Run Code Online (Sandbox Code Playgroud)

这似乎是一个兼容性/环境问题,我似乎无法弄清楚.有什么建议?

编辑:这是我的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
Run Code Online (Sandbox Code Playgroud)

nmo*_*ott 37

factory_girl_rails应该在gem文件spec_helper.rb而不是gem文件中需要gem - 你可能需要两次FactoryGirl,这就是你获得副本的原因.

在你的gem文件中试试这个:

group :test do
  gem "rspec"
  gem 'factory_girl_rails', :require => false
end
Run Code Online (Sandbox Code Playgroud)

然后确保spec_helper中需要工厂女孩:

require 'factory_girl_rails'
Run Code Online (Sandbox Code Playgroud)

顺便说一下 - 你不需要两个rspecrpsec-rails你的gemfile.您可以使用以下内容替换它们:

group :development, :test do
  gem 'rspec-rails'
end
Run Code Online (Sandbox Code Playgroud)

您需要在两个组中使用rspec,以便rake任务在开发中起作用,核心测试将在测试中起作用.


小智 22

我最近遇到了同样的问题.在我的情况下,/ factories中的一个文件有一个_spec.rb结尾(使用创意cp的结果).它装载了两次,首先是rspec,然后是工厂.


Edu*_*eal 16

我也有这个问题.在我的例子中,有两个文件具有相同的代码,如下所示:

FactoryGirl.define do
  factory :user do
  end
end
Run Code Online (Sandbox Code Playgroud)

一个文件名为"Useres.rb",另一个文件名为"User.rb",因此我只删除了"Useres.rb"并修复了错误.


Rya*_*ger 16

您是否有机会从配置文档中粘贴整个代码段以获取支持文件?

# 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
Run Code Online (Sandbox Code Playgroud)

如果你阅读了评论,你会看到你只想要一个或另一个.我犯了这个错误并得到了问题中陈述的错误.


Mor*_*rty 10

打电话FactoryGirl.define(:user)FactoryGirl.find_definitions两次你也有这个问题.

尝试删除第二个电话或:

FactoryGirl.factories.clear
FactoryGirl.find_definitions  
Run Code Online (Sandbox Code Playgroud)


Den*_*hin 9

另一个可能的原因是备用电话FactoryGirl.find_definitions.如果找到,请尝试删除find_definitions.


sca*_*er2 5

确保您的各个工厂文件没有以_spec.