2013-04-04 108 views
0

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 

如何測試持續時間應大於或等於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)>' 
+0

您能否澄清'沒有工作' - 您是否遇到異常或失敗?請發佈你看到的結果。 – PinnyM 2013-04-04 14:46:11

+0

謝謝你的建議。我編輯了問題。 – 2013-04-04 14:51:29

+0

您是否嘗試過http://rubydoc.info/gems/thoughtbot-shoulda/2.11.1/Shoulda/ActiveRecord/Matchers#ensure_inclusion_of-instance_method? – pjam 2013-04-04 14:53:42

回答

0

這看起來是一個已知的問題,現在,有一個拉請求爲了解決這個問題:

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

我建議你留意一下,看看它是否進入並檢查是否創建了新的gem。一旦它在git中,你也可以直接從那裏拉,而不用等待一個新的寶石。最後一種選擇是自己掏寶石,添加變化,並使用自己的寶石。

與此同時,如果您現在真的需要測試,您可以使用allow_value匹配器。