应该更大,更少的语法

时间:2013-04-04 14:38:55

标签: ruby-on-rails rspec shoulda

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)>'

1 个答案:

答案 0 :(得分:0)

这看起来是一个已知的问题,有一个拉取请求来解决这个问题:

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

我建议只关注它,看看它是否进入并检查创建新宝石的时间。一旦它在git中,你也可以直接从那里拉,而无需等待新的宝石。最后一种选择是自己分叉宝石,添加更改,并使用自己的宝石。

同时,如果您现在真的需要测试,可以使用allow_value匹配器。