-2

当我在功能/支持下更改 env.rb 文件时

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

并在 gemfile 源' https://rubygems.org '

gem 'rails', '3.2.13'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

group :test do
  gem 'cucumber-rails'
  gem 'capybara'
  gem 'database_cleaner'
  gem 'factory_girl_rails', :require => false
end

group :development, :test do
  gem 'rspec-rails'
end

gem 'jquery-rails'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

在规范/spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
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}

在规范/factories.rb

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

uninitialized constant FactoryGirl (NameError)
/home/manish/change/test_cucumber/spec/factories.rb:1:in `<top (required)>'
/home/manish/change/test_cucumber/features/support/env.rb:8:in `<top (required)>'
/home/manish/.rvm/gems/ruby-1.9.3-p392/gems/cucumber-1.3.5/lib/cucumber/rb_support/rb_language.rb:122:in `load'
/home/manish/.rvm/gems/ruby-1.9.3-p392/gems/cucumber-1.3.5/lib/cucumber/rb_support/rb_language.rb:122:in `load_code_file'
/home/manish/.rvm/gems/ruby-1.9.3-p392/gems/cucumber-1.3.5/lib/cucumber/runtime/support_code.rb:180:in `load_file'
/home/manish/.rvm/gems/ruby-1.9.3-p392/gems/cucumber-1.3.5/lib/cucumber/runtime/support_code.rb:83:in `block in load_files!'
/home/manish/.rvm/gems/ruby-1.9.3-p392/gems/cucumber-1.3.5/lib/cucumber/runtime/support_code.rb:82:in `each'
4

2 回答 2

10

在 spec/spec_helper.rb 尝试添加

FactoryGirl.find_definitions 

在下面

require 'factory_girl_rails'
于 2013-07-26T19:21:47.800 回答
1

我在 Hartl 教程中也遇到了这个问题。

如果您使用 Spork 来加速您的测试并且它在后台运行,您需要停止它 (Ctrl+C) 并重新启动它 ( $ bundle exec spork)。

于 2013-11-13T14:41:06.417 回答