1

在宝石文件中。我添加了 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

在 features/support/env.rb 需要来自 spec/factories.rb 的工厂

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

在规范/factories.rb

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

在 spec/spec_helper 我需要 require '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} 

现在我得到了以下错误...请建议我任何机构以及编写步骤定义以使用此 factory.rb。

manish@ubuntu:~/change/test_cucumber$ cucumber features/sign_up.feature 工厂已经注册:用户 (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:inregister' /home/manish/.rvm/gems/ruby-1.9.3-p392/gems/factory_girl-4.2.0/lib/factory_girl.rb:65:在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:infactory'/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:ininstance_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:indefine ' /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需要'/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:inload_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'

提前致谢。

4

2 回答 2

0

在我的情况下,rails_helper.rb我使用FactoryGirl.find_definitions了 ,结果证明这是多余的。

于 2014-12-20T17:58:06.333 回答
0

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

于 2013-07-26T11:53:43.540 回答