0

簡単な.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)>'
4

1 に答える 1

0

これは現在既知の問題のようで、これを修正するためにプル リクエストが行われています。

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

これに注目して、それが入っているかどうかを確認し、新しい宝石がいつ作成されるかを確認することをお勧めします. git に入ったら、新しい gem を待たずにそこから直接プルすることもできます。最後の代替手段は、gem を自分で fork し、変更を追加して、独自の gem を使用することです。

allow_valueそれまでの間、本当にテストが必要な場合はマッチャーを使用できます。

于 2013-04-04T17:32:33.337 に答える