#<Shoulda::Matchers 的 Rails Minitest 未定义方法 `greater_than_or_equal_to'

问题描述

我想在 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': #Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher:0x00007fba91da3ce0 (NoMethodError) 的未定义方法`greater_than_or_equal_to'

#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)

相关问答

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