【问题标题】:MiniTest Model Validation Test FailingMiniTest 模型验证测试失败
【发布时间】:2019-04-24 19:39:31
【问题描述】:

我有一个模型“政策”。在该模型中,我对 policy_holder 和 premium_amount 进行了存在验证。我正在尝试为此模型编写 MiniTest 测试。由于某种原因,我的测试失败了。

这是我的模型:

class Policy < ApplicationRecord
  belongs_to :industry
  belongs_to :carrier
  belongs_to :agent

  validates :policy_holder,  presence: true
  validates :premium_amount, presence: true
end

这是我的测试:

require 'test_helper'

class PolicyTest < ActiveSupport::TestCase
  test 'should validate policy holder is present' do
    policy = Policy.find_or_create_by(policy_holder: nil, premium_amount: '123.45',
                                      industry_id: 1, carrier_id: 1,
                                      agent_id: 1)
    assert_not policy.valid?
  end

  test 'should validate premium amount is present' do
    policy = Policy.find_or_create_by(policy_holder: 'Bob Stevens', premium_amount: nil,
                                      industry_id: 1, carrier_id: 1,
                                      agent_id: 1)
    assert_not policy.valid?
  end

  test 'should be valid when both policy holder and premium amount are present' do
    policy = Policy.find_or_create_by(policy_holder: 'Bob Stevens', premium_amount: '123.45',
                                      industry_id: 1, carrier_id: 1,
                                      agent_id: 1)
    assert policy.valid?
  end
end

这里是失败信息:

Failure:
PolicyTest#test_should_be_valid_when_both_policy_holder_and_premium_amount_are_present [test/models/policy_test.rb:22]:
Expected false to be truthy.

我认为应该通过的最后一个测试失败了。这让我觉得我的其他测试也不正确。

【问题讨论】:

    标签: ruby-on-rails testing minitest


    【解决方案1】:

    有一种更简单的方法来测试验证,涉及更少的“地毯式轰炸”:

    require 'test_helper'
    
    class PolicyTest < ActiveSupport::TestCase 
      setup do
        @policy = Policy.new
      end
    
      test "should validate presence of policy holder" do
        @policy.valid? # triggers the validations
        assert_includes(
          @policy.errors.details[:policy_holder],
          { error: :blank }
        )
      end
    
      # ...
    end
    

    这仅测试该验证,而不是模型上的所有验证组合。使用assert policy.valid? 不会告诉您错误消息中的失败原因。

    errors.details 是在 Rails 5 中添加的。在旧版本中您需要使用:

    assert_includes( policy.errors[:premium_amount], "can't be blank" )
    

    根据实际错误消息进行测试。或者你可以使用 active_model-errors_details 来支持该功能。

    【讨论】:

      【解决方案2】:

      所以这里发生的情况是模型上的验证失败。

      如果在运行验证时对象上没有错误,.valid? 将返回 true

      由于您清楚地看到“错误”,这意味着模型上的一个或多个验证失败。

      在 Rails 控制台中,您应该尝试手动创建对象并将其转换为变量,然后对其进行测试以查看错误:

      test = Policy.new(whatever params are needed to initialize here)
      # This will give you the object
      
      test.valid?
      #This will likely return FALSE, and NOW you can run:
      
      test.errors
      #This will actually show you where the validation failed inside the object
      

      无论如何,这几乎肯定是模型及其创建中的问题。

      请记住,在您对对象运行 .valid? 之后,.errors 才会起作用。

      【讨论】:

      • @hashrocket 您可以粘贴您的策略架构吗?是否可以像保费金额应该是浮点数而不是字符串一样简单?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 2017-05-06
      相关资源
      最近更新 更多