rspec 测试抛出未初始化的常量 Rails (NameError) [英] rspec testing throws Uninitialized constant Rails (NameError)

查看:41
本文介绍了rspec 测试抛出未初始化的常量 Rails (NameError)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Please forgive any shortcomings in this (my first-ever) post on StackOverflow. I'm brand new to Ruby on Rails. I'm following the Rails Tutorial. I have spent many unsuccessful hours consulting other threads discussing the same Name Error that I'm raising in this question.

我尝试像这样运行 rspec 测试:$bundle exec rspec spec/models/user_spec.rb抛出现在臭名昭著的错误:`': uninitialized constant Rails (NameError)

Any attempt of mine to run an rspec test like so: $bundle exec rspec spec/models/user_spec.rb throws the now infamous error: `': uninitialized constant Rails (NameError)

让我知道我是否应该向您提供更多信息以便让球滚动.

Let me know if there's any more information I should provide you in order to get the ball rolling.

这是我的 gemfile:

Here's my gemfile:

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 文件:

Here is my spec/models/user_spec.rb file:

require 'spec_helper'  

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

这是我的 app/models/user.rb 文件:
类用户

Here is my app/models/user.rb file:
class User < ActiveRecord::Base end

这是我的 spec_helper.rb 文件:

Here is my spec_helper.rb file:

# 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 已经存在)

I have definitely run bundle install. I can also confirm that I've already created the database and run the migration (db/test.sqlite3 already exists)

推荐答案

在你的 spec_helper.rb 中,你有以下一行两次:

In your spec_helper.rb, you have the following line twice:

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

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

Delete the first instance (the one on line 2). This is what is causing the error. Having this line before require 'rspec/rails' will cause problems because we don't know what Rails is, and so we cannot call the root method. The second instance (on line 13) is fine because this is after require 'rspec/rails'.

这篇关于rspec 测试抛出未初始化的常量 Rails (NameError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆