Rails minitest 在 has_many 验证中失败
Rails minitest failed in has_many validation
我有如下简单的 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_: associations should have many appropriateness_test_question. Expected AppropriatenessTestQuestion to have a has_many association called appropriateness_test_question (no association called appropriateness_test_question)
我错过了什么?
should have_many(:appropriateness_test_questions)
has_many协会使用复数,祝你好运!
已修复!我不小心检查了 appropriateness_test_questions
而不是 appropriateness_test_results
的关联
应该是:
should have_many(:appropriateness_test_results)
而不是:
should have_many(:appropriateness_test_questions)
我有如下简单的 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_: associations should have many appropriateness_test_question. Expected AppropriatenessTestQuestion to have a has_many association called appropriateness_test_question (no association called appropriateness_test_question)
我错过了什么?
should have_many(:appropriateness_test_questions)
has_many协会使用复数,祝你好运!
已修复!我不小心检查了 appropriateness_test_questions
而不是 appropriateness_test_results
应该是:
should have_many(:appropriateness_test_results)
而不是:
should have_many(:appropriateness_test_questions)