rspec测试抛出未初始化的常量Rails(NameError)

时间:2013-10-30 19:37:19

标签: ruby-on-rails testing rspec

请原谅StackOverflow上这篇(我的第一篇)帖子中的任何缺点。我是Ruby on Rails的新手。我正在关注Rails Tutorial。我花了很多不成功的时间咨询其他线程,讨论我在这个问题中提出的相同的名称错误。

我的任何尝试都是这样运行rspec测试:$ bundle exec rspec spec / models / user_spec.rb 抛出现在臭名昭着的错误:`':未初始化的常量Rails(NameError)

如果有任何更多信息我可以提供给你,以便让球滚动,请告诉我。

这是我的宝石文件:

source 'https://rubygems.org'  
ruby '2.0.0'  
    #ruby-gemset=railstutorial_rails_4_0  

gem 'rails', '4.0.0'  
gem 'bootstrap-sass', '2.3.2.0'  
gem 'bcrypt-ruby', '3.0.1'  
gem 'faker', '1.1.2'  
gem 'will_paginate', '3.0.4'  
gem 'bootstrap-will_paginate', '0.0.9'  

group :development, :test do
  gem 'sqlite3', '1.3.8'  
  gem 'rspec-rails', '2.13.1'  
  # The following optional lines are part of the advanced setup.  
  # gem 'guard-rspec', '2.5.0'  
  # gem 'spork-rails', '4.0.0'  
  # gem 'guard-spork', '1.5.0'  
  # gem 'childprocess', '0.3.6'  
end  

group :test do  
  gem 'selenium-webdriver', '2.35.1'  
  gem 'capybara', '2.1.0'  
  gem 'factory_girl_rails', '4.2.0'  
  gem 'cucumber-rails', '1.4.0', :require => false  
  gem 'database_cleaner', github: 'bmabey/database_cleaner'  

  # Uncomment these lines on Linux.  
  # gem 'libnotify', '0.8.0'  

  # Uncomment these lines on Windows.  
  # gem 'rb-notifu', '0.0.4'  
  # gem 'win32console', '1.3.2'  
  # gem 'wdm', '0.1.0'  
end  

gem 'sass-rails', '4.0.1'  
gem 'uglifier', '2.1.1'  
gem 'coffee-rails', '4.0.1'  
gem 'jquery-rails', '3.0.4'  
gem 'turbolinks', '1.1.1'  
gem 'jbuilder', '1.0.2'  

group :doc do  
  gem 'sdoc', '0.3.20', require: false  
end  

group :production do  
  gem 'pg', '0.15.1'  
  gem 'rails_12factor', '0.0.2'  
end  

这是我的spec / models / user_spec.rb文件:

require 'spec_helper'  

describe User do
  pending "add some examples to (or delete) #{__FILE__}"
end

这是我的app / models / user.rb文件:
    class User<的ActiveRecord :: Base的     端

这是我的spec_helper.rb文件:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

require 'test/unit'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

# 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 }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

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

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
  config.include Capybara::DSL
end

我肯定会运行bundle install。我也可以确认我已经创建了数据库并运行迁移(db / test.sqlite3已经存在)

2 个答案:

答案 0 :(得分:1)

spec_helper.rb中,您有以下行两次

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

删除第一个实例(第2行的实例)。这是造成错误的原因。 在require 'rspec/rails'之前使用此行会导致问题,因为我们不知道Rails是什么,因此我们无法调用root方法。 第二个实例(第13行)很好,因为它在require 'rspec/rails'之后。

答案 1 :(得分:0)

require 'spec_helper'文件中删除多余的spec_helper.rb行。