简体   繁体   English

Rails Minitest 未定义方法 `greater_than_or_equal_to' for # <shoulda::matchers< div><div id="text_translate"><p> 我想在 Minitest 中使用 ShouldaMatchers gem 来检查简单的 model 验证:</p><pre> class Portfolio &lt; ApplicationRecord validates:share_ratio, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100 } end</pre><p> 为此,我有一个 Minitest:</p><pre> require 'test_helper' class PortfolioTest &lt; 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</pre><p> 但我收到一个错误:</p><blockquote><p> class:PortfolioTest':#Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher:0x00007fba91da3ce0 (NoMethodError) 的未定义方法“greater_than_or_equal_to”</p></blockquote><pre> #Gemfile: gem 'shoulda', '~&gt; 4.0' gem 'shoulda-matchers', '~&gt; 4.0'</pre><p> 我尝试更改 model 内部的验证:</p><pre> validates_numericality_of:share_ratio, greater_than_or_equal_to: 0, less_than_or_equal_to: 100</pre><p> 但是错误是一样的。 </p></div></shoulda::matchers<>

[英]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.

相关问题 Rails:验证 Greater_than_or_equal_to 0 不起作用 - Rails: Validates greater_than_or_equal_to 0 does not work Shoulda Matchers - 未定义的方法gsub - Shoulda Matchers - undefined method gsub validates_numericality_of 中的 :greater_than_or_equal_to 仅在 Rails 3.1 中部分工作 - :greater_than_or_equal_to in validates_numericality_of only partially working in rails 3.1 如何在Rails中使用Minitest Specs风格的比喻匹配器? - How to use shoulda-matchers with Minitest Specs style in Rails? :greater_than_or_equal_to 、:数字和其他具有含义的符号 - :greater_than_or_equal_to, :numericality, and other symbols with meaning 带有rspec-rails 3.1.0和shoulda-matchers 2.7.0的Ruby on Rails 4.2.0未定义方法`validates_presence_of&#39; - Ruby on Rails 4.2.0 with rspec-rails 3.1.0 and shoulda-matchers 2.7.0 undefined method `validates_presence_of' for NoMethodError:未定义的方法`validate_presence_of'(Rspec和Shoulda-Matchers) - NoMethodError: undefined method `validate_presence_of' (Rspec and Shoulda-Matchers) Shoulda Matchers::last_name:Symbol的未定义方法`is_at_least&#39; - Shoulda Matchers: undefined method `is_at_least' for :last_name:Symbol Shoulda :: Matchers :: Independent :: DelegateMethodMatcher:未定义方法`allow_nil&#39; - Shoulda::Matchers::Independent::DelegateMethodMatcher: undefined method `allow_nil' 更新shoulda-matchers后的未定义方法&#39;assign_to&#39; - undefined method 'assign_to' after updating shoulda-matchers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM