为啥我在我的 Rspec 模型测试中得到单元化常量?

Posted

技术标签:

【中文标题】为啥我在我的 Rspec 模型测试中得到单元化常量?【英文标题】:Why am I getting unititialized constant in my Rspec model test?为什么我在我的 Rspec 模型测试中得到单元化常量? 【发布时间】:2022-01-01 13:53:40 【问题描述】:

我正在尝试在我的 rails 应用程序中使用 Rspec 创建一个测试,但我不断收到以下错误。

uninitialized constant Build
# ./spec/models/build_spec.rb:3:in `<top (required)>'
No examples found.

我正在运行 Rails 版本 6.1.4.1。我也知道我的模型存在,因为我可以在 ruby​​ 控制台中创建属于该表的新对象。

这是我的 rspec 文件 (spec/models/build_spec.rb)

require 'spec_helper'

RSpec.describe Build, type: :model do
  it "Has a name"
  it "is not valid without name"
  it "is not valid with too short name"
  it "is not valid with too long of a name"
end

这是我的宝石文件

source 'https://rubygems.org'
git_source(:github)  |repo| "https://github.com/#repo.git" 

ruby '2.7.4'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.4', '>= 6.1.4.1'

# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like javascript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

gem 'will_paginate', '~> 3.3'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.4', require: false

group :development, :test do
  # Use sqlite3 as the database for Active Record
  gem 'sqlite3', '~> 1.4.2'

  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 4.1.0'
  # Display performance information such as SQL time and flame graphs for each request in your browser.
  # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
  gem 'rack-mini-profiler', '~> 2.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 3.26'
  gem 'selenium-webdriver'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
  gem 'rspec-rails', '>= 3.9.0'
end

group :production do 
  gem 'pg', '~> 1.1'
end


# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

【问题讨论】:

您在app/models/ 目录中有构建模型吗?似乎规范无法找到构建模型。 【参考方案1】:

检查app/models/ 目录中的模型是否正确。

spec/models/build_spec.rb

require 'rails_helper'

RSpec.describe Build, type: :model do
  it  expect(described_class.new).to be_a() 
end

app/models/build.rb

class Build < ApplicationRecord
end

希望这对你有用。

【讨论】:

【参考方案2】:

spec_helper 不加载环境(这包括不加载模型)。您可以在本规范中明确要求模型文件,也可以改为要求 rails_helper

【讨论】:

我之前尝试过,所以我很困惑,直到我得出结论,我以某种方式丢失了 rails_helper.rb 文件。所以解决方案是运行 gem uninstall rspec-rails,然后再次重新安装 rspec,现在我的测试正在运行。谢谢你的解释。

以上是关于为啥我在我的 Rspec 模型测试中得到单元化常量?的主要内容,如果未能解决你的问题,请参考以下文章

尝试在Rails中设置rspec测试时获取未初始化的常量Faker :: String

RSpec 测试不允许两个用户使用相同的电子邮件地址

Rspec 数据库视图为空

为啥每次单元测试后我的数据库都没有被清除?

为啥在我的单元测试中主源代码树中的“内部”类型不可见?

在 RSpec 中测试 Rails 视图:为啥当我想测试“索引”时它会路由到“显示”?