Rspec没有看到我的模型类。 未初始化的常量错误

我在Ruby on Rails应用程序中为我的模型编写Rspectesting。 而我收到这个错误,而开始'rspec规范'

command: /spec/models/client_spec.rb:4:in `<top (required)>': uninitialized constant Client (NameError) 

我使用Rails 4.0.0和Ruby 2.0.0

这是我的client_spec.rb:

 require 'spec_helper' describe Client do it 'is invalid without first_name', :focus => true do client = Client.new client.should_not be_valid end end 

和Gemfile:

 source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.0.0.rc1' # Use sqlite3 as the database for Active Record gem 'sqlite3' # Use SCSS for stylesheets gem 'sass-rails', '~> 4.0.0.rc1' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .js.coffee assets and views gem 'coffee-rails', '~> 4.0.0' # gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: gem 'turbolinks' gem 'jbuilder', '~> 1.0.1' group :development do gem 'rspec-rails' end group :doc do # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', require: false end group :test do gem 'rspec-rails' gem 'factory_girl_rails' gem 'database_cleaner' end 

最后client.rb(ROR Model and Class):

 class Client < ActiveRecord::Base has_many :cars has_many :orders has_one :client_status has_one :discount_plan, through: :client_status validates :email, format: { with: /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[az]{2,4})\z/, :message => "Only emails allowed", :multiline => true } validates :email, presence: true, if: "phone.nil?" #validates :phone, presence: true, if: "email.nil?" validates :last_name, :first_name, presence: true validates :last_name, :first_name, length: { minimum: 2, maximum: 500, wrong_length: "Invalid length", too_long: "%{count} characters is the maximum allowed", too_short: "must have at least %{count} characters" } end 

如果这将是有用的我的spec_helper.rb文件:

 # This file was generated by the `rspec --init` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # Require this file using `require "spec_helper"` to ensure that it is only # loaded once. # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| config.treat_symbols_as_metadata_keys_with_true_values = true config.run_all_when_everything_filtered = true config.filter_run :focus # 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.use_transactional_fixtures = false config.before(:suite) do DatabaseCleaner.strategy = :transaction DatabaseCleaner.clean_with(:truncation) end config.before(:each) do DatabaseCleaner.start end config.after(:each) do DatabaseCleaner.clean end end 

您的spec_helper文件缺less一些重要的命令。 具体来说,它不包括config / environment和初始化rspec-rails

您可以spec/spec_helper.rb添加到spec/spec_helper.rb文件的开头

 ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' 

或者你可以运行

 rails generate rspec:install 

并使用rspec-rails使用生成的spec_helper覆盖您的spec_helper

在rails 4.x(rspec-rails 3.1.0)中使用

 require "rails_helper" # this 

 require "spec_helper" # not this 

在你的spec文件中

您可能--require rails_helper在您的.rspec文件中添加--require rails_helper ,使其看起来像这样。

 --color --require spec_helper --require rails_helper 

在此之后,您不需要在所有规格中都要求使用rails_helper。

我正在使用Rails 5.0.0.1。
这是我如何解决这个问题。

在您的Gemfile中,请添加 – > gem'rspec -rails',“> = 2.0.0.beta”

像这样,

 group :development, :test do gem 'rspec-rails', ">= 2.0.0.beta" end 

原因:如果没有添加rspec-rails,并且执行rspec命令时,会产生这个错误 – > “无法加载这样的文件 – rails_helper”

现在在terminal上执行这个命令。

捆绑安装

一旦bundle命令执行良好,执行rails generate。 像这样,

rails生成rspec:install

原因:这个命令会创build一个新的.rspec(当提示时点击覆盖),spec / rails_helper.rb和spec / spec_helper.rb

现在,在这一点上,rspec应该可以正常运行。
然而,如果你遇到一个错误,在模型没有find,例如无法加载这样的文件 – 想法 ,尝试添加这个顶部的spec / spec_helper.rb

 require 'rubygems' ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) 

原因:似乎spec_helper没有加载Rails环境。 我们要求它。

希望这可以帮助!

事情已经移动了一点,因为这个线程已经创build,我也经历了使用Ruby 2.1,Rails 4.2,rspec-rails 3.3的uninitialized constant ClassName (NameError)错误。

我已经解决了阅读rspec-rails gem文档的问题:

https://github.com/rspec/rspec-rails#model-specs

它确认了Swards所说的关于要求“rails_helper”而不是“spec_helper”的说法。

此外,我的模型规范看起来更像是从gem文档

 RSpec.describe Url, :type => :model do it 'is invalid without first_name', :focus => true do client = Client.new client.should_not be_valid end end