【问题标题】:Rails minitest failed in has_many validationRails minitest 在 has_many 验证中失败
【发布时间】: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


    【解决方案1】:
    should have_many(:appropriateness_test_questions)
    

    has_many 关联使用复数,祝你好运!

    【讨论】:

      【解决方案2】:

      修好了!我不小心检查了appropriateness_test_questions 而不是appropriateness_test_results 的关联

      应该是:

      should have_many(:appropriateness_test_results)
      

      代替:

      should have_many(:appropriateness_test_questions)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多