【问题标题】:NoMethodError: undefined method `belong_to' for test when using shoulda-matchers in unit testNoMethodError:在单元测试中使用 shoulda-matchers 时未定义的测试方法“belong_to”
【发布时间】:2020-02-13 16:06:14
【问题描述】:

我尝试使用 shoulda-matchers 来测试模型之间的关联。但是,它总是显示错误: 参加测试#test_belongs_to: NoMethodError:未定义的方法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

test/models/taking_test.rb

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

【问题讨论】:

标签: ruby-on-rails ruby shoulda shoulda-matchers


【解决方案1】:

同时拥有spec 目录和test 目录可能会导致您的问题。 IMO,每个项目都应该只有spectest,不能同时使用。

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

require 'test_helper'

你有一个spec_helper,而不是test_helper

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

【讨论】:

    猜你喜欢
    • 2016-03-13
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多