[英]Rails Minitest undefined method `greater_than_or_equal_to' for #<Shoulda::Matchers
I want to use ShouldaMatchers gem inside of Minitest to check simple model validation:我想在 Minitest 中使用 ShouldaMatchers gem 来检查简单的 model 验证:
class Portfolio < ApplicationRecord
validates :share_ratio, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100 }
end
To do so I've got a Minitest:为此,我有一个 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
But I'm getting an error:但我收到一个错误:
class:PortfolioTest': undefined method `greater_than_or_equal_to' for #Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher:0x00007fba91da3ce0 (NoMethodError) class:PortfolioTest':#Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher:0x00007fba91da3ce0 (NoMethodError) 的未定义方法“greater_than_or_equal_to”
#Gemfile:
gem 'shoulda', '~> 4.0'
gem 'shoulda-matchers', '~> 4.0'
I tried to change validation inside of the model:我尝试更改 model 内部的验证:
validates_numericality_of :share_ratio, greater_than_or_equal_to: 0, less_than_or_equal_to: 100
But error is the same.但是错误是一样的。
You got one of them right, but not the other.你猜对了其中一个,但不是另一个。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.