【发布时间】: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
【问题讨论】:
-
您遵循了将 Shoulda 与 RSpec 集成的说明,但那是一个 Minitest 测试。您是在尝试编写测试或规范,还是只是不知道其中的区别? github.com/thoughtbot/shoulda-matchers#minitest
标签: ruby-on-rails ruby shoulda shoulda-matchers