未初始化的常量FactoryGirl(NameError)

时间:2013-07-26 11:23:51

标签: ruby-on-rails cucumber

当我更改feature / support

下的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 / 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}

在spec / 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'

2 个答案:

答案 0 :(得分:10)

在spec / spec_helper.rb中尝试添加

FactoryGirl.find_definitions 

下的

require 'factory_girl_rails'

答案 1 :(得分:1)

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

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