Rspec没有看到我的模型Class.未初始化的常量错误 [英] Rspec doesn't see my model Class. uninitialized constant error

查看:55
本文介绍了Rspec没有看到我的模型Class.未初始化的常量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ruby on Rails应用程序中针对我的模型在Rspec上编写测试. 并且在启动'rspec spec'

I'm writing tests on Rspec for my models in Ruby on Rails application. And I receive this error while starting 'rspec spec'

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

我使用Rails 4.0.0和Ruby 2.0.0

I use Rails 4.0.0 and Ruby 2.0.0

这是我的client_spec.rb:

Here is my 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模型和类):

And at last 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-]+)*(\.[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_helper.rb文件有用的话:

If it'd be useful my spec_helper.rb file:

# 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和初始化rspec-rails.

Your spec_helper file is missing some important commands. Specifically, it's not including config/environment and initializing rspec-rails.

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

You can add the following lines to the start of your spec/spec_helper.rb file

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.

and overwrite your spec_helper with one generated for use with rspec-rails.

这篇关于Rspec没有看到我的模型Class.未初始化的常量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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