NameError:未初始化的常量Shoulda



我做这个教程https://www.digitalocean.com/community/tutorials/build-a-restful-json-api-with-rails-5-part-one

但是当在model一章运行RSpec测试时,我得到以下错误:

C:UsersNCH-Lap10Desktoprubyprojectstodos-api>bundle exec rspec
An error occurred while loading ./spec/controllers/items_controller_spec.rb.
Failure/Error:
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
NameError:
uninitialized constant Shoulda
# ./spec/rails_helper.rb:6:in `<top (required)>'
# ./spec/controllers/items_controller_spec.rb:1:in `require'
# ./spec/controllers/items_controller_spec.rb:1:in `<top (required)>'
An error occurred while loading ./spec/controllers/todos_controller_spec.rb.
Failure/Error:
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
NameError:
uninitialized constant Shoulda
# ./spec/rails_helper.rb:6:in `<top (required)>'
# ./spec/controllers/todos_controller_spec.rb:1:in `require'
# ./spec/controllers/todos_controller_spec.rb:1:in `<top (required)>'
An error occurred while loading ./spec/models/item_spec.rb.

所以他找不到我的测试。(我从教程中复制的)

我的测试位于规格/模型中,看起来像:

require 'rails_helper'
# Test suite for the Item model
RSpec.describe Item, type: :model do
# Association test
# ensure an item record belongs to a single todo record
it { should belong_to(:todo) }
# Validation test
# ensure column name is present before saving
it { should validate_presence_of(:name) }
end

我的Gemfile的部分:

group :test do
gem 'factory_bot_rails', '~> 4.0'
gem 'shoulda-matchers', '~> 3.1'
gem 'faker'
gem 'database_cleaner'
end

我应该提供更多的代码吗?

根据github上的这个问题,

您可能需要将此添加到您的spec_helper.rb:

require "shoulda/matchers"
require "shoulda/matchers/integrations/rspec"

另外,确保spec_helperrails_helper中是必需的(并且rails_helper在测试文件中是必需的)。

为将来的读者编辑:第二行可能在最近的Rails版本中被弃用(见注释),所以不要添加它

相关内容

  • 没有找到相关文章

最新更新