黄瓜测试期间工厂已经注册:user(FactoryGirl :: DuplicateDefinitionError)

时间:2013-07-26 11:44:24

标签: ruby-on-rails cucumber factory-bot

宝石文件中的

。我添加了factory_girls_rails。

group :test do
  gem 'cucumber-rails'
  gem 'capybara'
  gem 'database_cleaner'
  gem 'factory_girl_rails'
end

group :development, :test do
  gem 'rspec-rails'
end
feature / support / env.rb中的

需要来自spec / factories.rb的工厂

require 'cucumber/rails'
require "#{Rails.root}/spec/factories"

在spec / factories.rb

FactoryGirl.define do
  factory :user do
    first_name "John"
    last_name  "Doe"
    admin false
  end
end

并且在spec / spec_helper中我要求'factory_girl_rails'

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} 

现在我得到了以下错误... plz建议我任何身体也写下步骤定义使用这个factory.rb。

manish @ ubuntu:〜/ change / test_cucumber $ cucumber features / sign_up.feature 工厂已经注册:user(FactoryGirl :: DuplicateDefinitionError) /home/manish/.rvm/gems/ruby-1.9.3-p392/gems/factory_girl-4.2.0/lib/factory_girl /decorator.rb:10:in method_missing' /home/manish/.rvm/gems/ruby-1.9.3-p392/gems/factory_girl-4.2.0/lib/factory_girl/decorator/disallows_duplicates_registry.rb:6:in注册' /home/manish/.rvm/gems/ruby-1.9.3-p392/gems/factory_girl-4.2.0/lib/factory_girl.rb:65:in block in register_factory' /home/manish/.rvm/gems/ruby-1.9.3-p392/gems/factory_girl-4.2.0/lib/factory_girl.rb:64:in每个' /home/manish/.rvm/gems/ruby-1.9.3-p392/gems/factory_girl-4.2.0/lib/factory_girl.rb:64:in register_factory' /home/manish/.rvm/gems/ruby-1.9.3-p392/gems/factory_girl-4.2.0/lib/factory_girl/syntax/default.rb:20:in factory' /home/manish/change/test_cucumber/spec/factories.rb:2:in block in <top (required)>' /home/manish/.rvm/gems/ruby-1.9.3-p392/gems/factory_girl-4.2.0/lib/factory_girl/syntax/default.rb:49:in instance_eval' /home/manish/.rvm/gems/ruby-1.9.3-p392/gems/factory_girl-4.2.0/lib/factory_girl/syntax/default.rb:49:in run' /home/manish/.rvm/gems/ruby-1.9.3-p392/gems/factory_girl-4.2.0/lib/factory_girl/syntax/default.rb:7:in define' /home/manish/change/test_cucumber/spec/factories.rb:1:in <top (required)>' /home/manish/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in require' /home/manish/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in block in require' /home/manish/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in load_dependency' /home/manish/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in require' /home/manish/change/test_cucumber/features/support/env.rb:8:in'

提前感谢。

2 个答案:

答案 0 :(得分:0)

我认为要求factory_girl_rails已经加载了工厂文件,因此在support / env文件中再次要求导致冲突。删除support / env.rb中的第二行,看看会发生什么。

答案 1 :(得分:0)

在我rails_helper.rb的案例中,我使用FactoryGirl.find_definitions,结果证明是多余的。