我尝试使用应该匹配器来测试模型之间的关联。但是,它始终显示错误: 服用测试#test_belongs_to: NoMethodError: 未定义的方法belong_to' for #<TakingTest:0x00007fc14b8e64a8>
test/models/taking_test.rb:8:in
' 中的块 我检查了其他答案,其中大多数至少是 4 年前。它仍然适用于 rails 6.0 吗?
红宝石 '2.6.5'
导轨"、"~> 6.0.2">
宝石文件
group :development, :test do
gem 'rspec-rails'
end
group :test do
gem 'shoulda', '~> 3.5'
gem 'shoulda-matchers'
end
规格/rails_helper.rb:
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
测试/模型/taking_test.rb
class TakingTest < ActiveSupport::TestCase
test "belongs to" do
should belong_to(:students)
end
end
同时拥有spec
目录和test
目录可能会导致您的问题。IMO,每个项目都应该只有spec
或test
,而不是两者兼而有之。
通常,测试文件以test_helper.rb
文件的包含开头。
require 'test_helper'
你得到的不是test_helper
,而是spec_helper
。
尝试将应该具有初始值设定项从spec/spec_helper.rb
移动到test/test_helper.rb