Rails #<Shoulda::Matchers 的最小未定义方法“greater_than_or_equal_to”

Rails Minitest undefined method `greater_than_or_equal_to' for #<Shoulda::Matchers

我想在 Minitest 中使用 ShouldaMatchers gem 来检查简单模型验证:

class Portfolio < ApplicationRecord
  validates :share_ratio, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100 }
end

为此,我有一个 Minitest:

require 'test_helper'

class PortfolioTest < ActiveSupport::TestCase
  context 'validations' do
    should validate_numericality_of(:share_ratio).greater_than_or_equal_to(0)
    should validate_numericality_of(:share_ratio).is_less_than_or_equal_to(100)
  end
end

但是我收到一个错误:

class:PortfolioTest': undefined method `greater_than_or_equal_to' for #Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher:0x00007fba91da3ce0 (NoMethodError)

#Gemfile:
  gem 'shoulda', '~> 4.0'
  gem 'shoulda-matchers', '~> 4.0'

我尝试更改模型内部的验证:

validates_numericality_of :share_ratio, greater_than_or_equal_to: 0, less_than_or_equal_to: 100

但错误是一样的。

你答对了其中一个,但另一个不对。

should validate_numericality_of(:share_ratio).is_greater_than_or_equal_to(0)
should validate_numericality_of(:share_ratio).is_less_than_or_equal_to(100)