如何解决Rails minitest 在 has_many 验证中失败
我有如下简单的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
这给了我一个错误:
失败:AppropriatenessTestQuestionTest#test_:关联应该有很多properties_test_question。预期的 AppropriatenessTestQuestion 有一个 has_many 关联,称为propertyness_test_question(没有关联,称为propertyness_test_question)
我错过了什么?
解决方法
select t.*
from (select t.*,lead(date) over (partition by location,product order by date) as next_lp_date,lead(date) over (order by date) as next_date
from t
) t
where next_lp_date is null or next_lp_date <> next_date
has_many 关联使用复数,祝你好运!
,解决了!我不小心检查了 appropriateness_test_questions
而不是 appropriateness_test_results
应该是:
should have_many(:appropriateness_test_results)
代替:
should have_many(:appropriateness_test_questions)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。