【问题标题】:Getting error: undefined method `greater_than_or_equal_to' for Shoulda::Matchers出现错误:Shouda::Matchers 的未定义方法“greater_than_or_equal_to”
【发布时间】:2013-01-17 16:00:37
【问题描述】:

这是我想要通过的规范:

context "when validating format of attributes" do
    it { should validate_numericality_of(:price).greater_than_or_equal_to(0.01) }
end

我安装了rspec 和shoulda-matchers。但我收到undefined methodgreater_than_or_equal_to'`

现在该方法不在文档中,但它确实存在于此处:

https://github.com/moffff/shoulda-matchers/commit/7da06487d25c27d59d11fb7f2962e7ff345e45c4

那么为什么这不起作用?我应该怎么做才能让它工作?

【问题讨论】:

    标签: ruby-on-rails-3 unit-testing activerecord rspec2 shoulda


    【解决方案1】:

    该方法在您应用中的原始thoughtbot/shoulda-matchers 中不存在。您所指的版本是原始 gem 的一个分支。

    你可以用这个 gem 代替原来的 gem,只需在 Gemfile 中设置它的源代码

    gem 'shoulda-matchers', :git => 'https://github.com/moffff/shoulda-matchers.git'
    

    但您应该了解,分叉和修改的版本可能会不稳定,不包括最新的更新和修复,等等。

    【讨论】:

    • 这就是你使用分叉 gem 的方式。谢谢
    【解决方案2】:

    它必须是is_greater_than_or_equal_to(带有is_ 前缀),而不是greater_than_or_equal_to。

    查看文档:https://matchers.shoulda.io/docs/v4.1.2/Shoulda/Matchers/ActiveModel.html#validate_numericality_of-instance_method

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 2019-11-17
      • 2014-09-18
      • 2015-01-15
      相关资源
      最近更新 更多