0

我尝试使用 shoulda-matchers 来测试模型之间的关联。但是,它总是显示 Error: TakeTest#test_belongs_to: NoMethodError: undefined method belong_to' for #<TakingTest:0x00007fc14b8e64a8> test/models/taking_test.rb:8:inblock 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

测试/模型/take_test.rb

class TakingTest < ActiveSupport::TestCase
   test "belongs to" do
     should belong_to(:students)
   end
end
4

1 回答 1

0

同时拥有一个spec目录和一个test目录可能会导致您的问题。IMO,每个项目都应该只有一个spec或一个,而不是两者兼而有之。test

通常,测试文件以文件的包含test_helper.rb开头。

require 'test_helper'

而不是 a test_helper,你有一个spec_helper.

尝试将 Shoulda 初始化程序从 移动spec/spec_helper.rbtest/test_helper.rb

于 2020-02-13T22:58:32.660 回答