【问题标题】:shoulda greater and less than syntax应该大于和小于语法
【发布时间】:2013-04-04 14:38:55
【问题描述】:

brief.rb

# encoding: utf-8
class Brief < ActiveRecord::Base
  belongs_to :project
  validate :can_only_be_edited_if_project_is_not_started
  validates_presence_of :project, :inverse_of => :brief

  validates_numericality_of :duration, :only_integer => true, :less_than_or_equal_to => 15, :greater_than_or_equal_to => 5

  validates_length_of :brand_info, :maximum => 4000

  def can_only_be_edited_if_project_is_not_started
    errors.add(:base, 'Proje can't edit if duration is end!') if
      !project.nil? && !project.brief_can_be_edited?
  end

end

如何使用 shoulda gem 测试持续时间应该大于或等于 5 且小于或等于 15?

我试过下面的代码

brief_spec.rb

it { should ensure_inclusion_of(:duration).in_range(5..15)

但我收到了这个错误

1) Brief non-specific tests 
     Failure/Error: it { should ensure_inclusion_of(:duration).in_range(5..15) }
       Did not expect errors to include "It's not suitable word" when duration is set to 4, got error: 
     # ./spec/models/brief_spec.rb:23:in `block (3 levels) in <top (required)>'

【问题讨论】:

  • 你能澄清一下'没用' - 你是遇到异常还是失败?请发布您看到的结果。
  • 感谢您的建议。我编辑了问题。
  • 请提供正在测试的模型的代码。该错误似乎表明某些配置不正确。
  • 我添加了整个brief.rb模型

标签: ruby-on-rails rspec shoulda


【解决方案1】:

这似乎是一个已知问题,已提出拉取请求以解决此问题:

https://github.com/thoughtbot/shoulda-matchers/pull/281

我建议只关注它以查看它是否进入并查看何时创建新宝石。一旦它在 git 中,您也可以直接从那里提取,而无需等待新的 gem。最后一种选择是自己派生 gem,添加更改,然后使用自己的 gem。

与此同时,如果您现在确实需要在那里进行测试,可以使用 allow_value 匹配器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-06
    • 2017-05-02
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 2018-09-27
    • 1970-01-01
    相关资源
    最近更新 更多