【发布时间】:2021-04-08 16:57:08
【问题描述】:
我有如下简单的 belongs_to/has_many 关系:
class AppropriatenessTestResult < ApplicationRecord
belongs_to :appropriateness_test_question
end
class AppropriatenessTestQuestion < ApplicationRecord
has_many :appropriateness_test_results, dependent: :destroy
end
为AppropriatenessTestResult迁移:
class CreateAppropriatenessTestResults < ActiveRecord::Migration[6.1]
def change
create_table :appropriateness_test_results do |t|
t.references :appropriateness_test_question, foreign_key: true, index: { name: 'idx_appropriateness_test_question_id' }
t.timestamps
end
end
end
现在我想测试关系是否设置正确。为此,我为 AppropriatenessTestQuestion 模型编写了 Minitest:
require 'test_helper'
class AppropriatenessTestQuestionTest < ActiveSupport::TestCase
context 'associations' do
should have_many(:appropriateness_test_question)
end
end
这给了我一个错误:
Failure: AppropriatenessTestQuestionTest#test_:关联应该有许多适当性_test_question。预期 AppropriatenessTestQuestion 有一个 has_many 关联,称为适当性测试问题(没有关联称为适当性测试问题)
我错过了什么?
【问题讨论】:
标签: ruby-on-rails ruby minitest