Rails Minitest one model validation causes ArgumentError: You need to supply at least one validation

Rails Minitest one model validation causes ArgumentError: You need to supply at least one validation

在我的 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: You need to supply at least one validation

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

  validates :initial_return do |record, attr, value|
    record.errors.add(attr, '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

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

  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

如您所见,测试失败不会告诉您模型为何 valid/invalid。

改为每次测试使用一个断言并测试实际验证:

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) 在设置块中预计已经失败。

我不知道这是否会导致实际错误,但如果您不提供初始 initial_return,则无法 create 对象。因为它 运行 反对验证本身。

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

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

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

或者,如果您想将对象持久保存在数据库中,则必须确保设置对象有效

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