问题描述
我想在 Minitest 中使用 Shouldamatchers gem 来检查简单的模型验证:
class Portfolio < ApplicationRecord
validates :share_ratio,numericality: { greater_than_or_equal_to: 0,less_than_or_equal_to: 100 }
end
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)