【问题标题】:Rails Minitest undefined method `greater_than_or_equal_to' for #<Shoulda::MatchersRails Minitest 未定义方法 `greater_than_or_equal_to' 用于 #<Shoulda::Matchers
【发布时间】:2021-05-11 13:26:20
【问题描述】:

我想在 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

但是错误是一样的。

【问题讨论】:

    标签: ruby-on-rails ruby minitest shoulda


    【解决方案1】:

    其中一个是正确的,但另一个是错误的。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-13
      • 2016-08-22
      • 2015-03-17
      • 2013-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多