Rails Minitest 一种模型验证会导致 ArgumentError:您需要提供至少一种验证

问题描述

在我的 Rails6 应用程序中,我有两个模型验证要通过 Minitest 进行测试:

class Portfolio < ApplicationRecord
  validates :name,:status,presence: true
  validates :initial_return do |record,attr,value|
    record.errors.add(attr,'Add value between -100 and 100') unless value >= -100 && value <= 100
  end
end

迷你测试:

class PortfolioTest < ActiveSupport::TestCase
  setup do
    @portfolio = Portfolio.create(name: Faker::Bank.name)
  end

  test 'invalid PortfolioSpotlightfigure,does not fit the range (-100,100)' do
    @portfolio.initial_return = -101
    assert_not @portfolio.valid?
    @portfolio.initial_return = 101
    assert_not @portfolio.valid?
    @portfolio.initial_return = 50
    assert @portfolio.valid?
  end

  context 'validations' do
    should validate_presence_of(:name)
  end
end

Minitest 在两种情况下都给出了相同的错误

ArgumentError:您需要提供至少一个验证

但是当我从 :initial_return 模型中删除 Portfolio 字段的验证时:

  validates :initial_return do |record,'Add value between -100 and 100') unless value >= -100 && value <= 100

validate_presence_of(:name) 的测试将通过,这意味着我错误地定义了该验证。我错过了什么?

解决方法

你不需要重新发明轮子

class Portfolio < ApplicationRecord
  validates :name,:status,presence: true
  validates :initial_return,numericality: {
      greater_than_or_equal_to: -100,less_than_or_equal_to: 100
    }
end

并停止在测试中对验证进行地毯式轰炸。测试实际验证,而不是整个对象是否有效/无效,这会导致误报和否定。例如:

  test 'invalid PortfolioSpotlightFigure,does not fit the range (-100,100)' do
    @portfolio.initial_return = -101
    # these will pass even if you comment out the validation on initial_return as 
    # status is nil
    assert_not @portfolio.valid? 
    @portfolio.initial_return = 101
    assert_not @portfolio.valid?
    # Will fail because status is nil
    @portfolio.initial_return = 50
    assert @portfolio.valid?
  end

正如您所见,测试失败不会告诉您模型有效/无效的原因。

相反,每个测试使用一个断言并测试实际验证:

class PortfolioTest < ActiveSupport::TestCase
  setup do
    # you dont need to insert records into the db to test associations
    @portfolio = Portfolio.new
  end

  test 'initial return over 100 is invalid' do
    # arrange
    @portfolio.initial_return = 200
    # act 
    @portfolio.valid?
    # assert
    assert_includes(@portfolio.errors.full_messages,"Initial return must be less than or equal to 100")
  end

  test 'initial return below -100 is invalid' do
    # arrange
    @portfolio.initial_return = -200
    # act 
    @portfolio.valid?
    # assert
    assert_includes(@portfolio.errors.full_messages,"Initial return must be greater than or equal to -100")
  end

  test 'an initial return between -100 and 100 is valid' do
    # arrange
    @portfolio.initial_return = 50
    # act 
    @portfolio.valid?
    # assert
    refute(@portfolio.errors.has_key?(:intial_return))
  end

  # ...
end

使用 shoulda,您应该可以使用 validates_numericality_of matcher

should validate_numericality_of(:initial_return).
            is_greater_than_or_equal_to(-100).
            is_less_than_or_equal_to(100)
, 安装块中的

@portfolio = Portfolio.create(name: Faker::Bank.name) 预计已经失败。

我不知道这是否会导致实际错误,但是当您不提供初始 create 时,您无法 initial_return 对象。因为它与验证本身背道而驰。

因为运行数字范围的测试用例,您需要确保初始对象有效。 这就是为什么当您删除 initial_return 验证时它没有失败的原因,因为 setup 块在没有验证的情况下成功。你只是看错了结局。

因此您要么使用 build,它不会将对象持久保存在数据库中,并且最初不会运行验证

@portfolio = Portfolio.build(name: Faker::Bank.name)

或者如果你想在数据库中持久化对象,你必须确保设置对象是有效的

@portfolio = Portfolio.create(name: Faker::Bank.name,initial_return: 50)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...