我正在为Ruby on Rails应用程序中的模型编写Rspec测试.

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

我使用Rails 4.0.0和Ruby 2.0.0

以下是我的客户_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

和文件:

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

最后是客户.rb(ROR模型和等级):

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-]+)*(\.[a-z]{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_助手.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文件缺少一些重要的命令.具体来说,它不包括config/environment和initializerspec-rails.

您可以在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.

Ruby-on-rails相关问答推荐

涡轮帧SRC已更新,但html未呈现

在Ruby on rails中,重复访问ActiveRecordModel返回相同的对象:预期行为还是错误?

将两个日期之间的差值转换为Ruby中的iso8601持续时间(如果需要,还可以使用Rails)

RSpec系统测试无法从工厂访问新创建的记录

您如何查看在 Rails 中触发回调的关联模型?

Rails:关于yields

无法在生产服务器上启动 Rails 4 控制台

jQuery ajax 请求不会触发 Rails 控制器的 JS 响应?

Rails 和 OS X:如何安装 rmagick?

搭建脚手架时建立关系

如何将字符串转换为类方法?

如何让空白复选框作为假传递给参数

Rails 5:无法从参数中检索哈希值

Rails 3.0 中的 f.error_messages

强参数需要多个

Active Record has_many:通过删除一条关联记录

Rails 3.1 中的 Rails.cache 错误 - TypeError: can't dump hash with default proc

如何在每个操作的基础上禁用 Ruby on Rails 的日志(log)记录?

将新管理员添加到活动管理员

Ruby on Rails Bootstrap Glyphicons 不工作