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

How can I test duration should greater than or equal to 5 and less than or equal to 15 with shoulda gem?

I tried below code

brief_spec.rb

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

but I got this error

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)>'
Johnny Cash
  • 4,867
  • 5
  • 21
  • 37
  • Can you clarify 'didn't work' - did you get an exception or a failure? Please post the results you are seeing. – PinnyM Apr 04 '13 at 14:46
  • did you try http://rubydoc.info/gems/thoughtbot-shoulda/2.11.1/Shoulda/ActiveRecord/Matchers#ensure_inclusion_of-instance_method ? – pjam Apr 04 '13 at 14:53
  • Please provide the code for the model being tested. The error seems to indicate something wasn't configured properly. – PinnyM Apr 04 '13 at 14:56
  • I added whole brief.rb model – Johnny Cash Apr 04 '13 at 14:59
  • Where are you setting duration to 4? – PinnyM Apr 04 '13 at 15:01
  • Anywhere.If I change in_range(5..15) to in_range(6..15) the error is that "when duration is set to 5, got error" – Johnny Cash Apr 04 '13 at 15:03
  • I mean can you display the test code setting the models/values that are being tested? – PinnyM Apr 04 '13 at 15:05
  • @PinnyM All you need to see of the test is already being shown in the question. That simple shoulda matcher will figure out if that range is allowed, and one of the things it does is the takes the minimum range value (so 5 in this case), subtracts 1, and tests if it can use that number. That's why the error indicates it was using 4 for the duration. – MrDanA Apr 04 '13 at 17:31
  • @MrDanA - right, except the error seems to indicate that something else is wrong as well which is why I want to know how this test is being setup. – PinnyM Apr 04 '13 at 17:55

1 Answers1

0

This looks to be a known issue right now, with a pull request made to fix this:

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

I suggest just keeping an eye on this to see if it goes in and check to see when a new gem is created. Once it's in git, you could also pull directly from there without waiting for a new gem. A last alternative would be to fork the gem yourself, add the changes, and use your own gem.

In the mean time, you could use the allow_value matcher if you really needed a test there now.

MrDanA
  • 11,489
  • 2
  • 36
  • 47