Any special assertion to test if the resulting integer lies within a range

Go To StackoverFlow.com

1

I would like to test if an instance variable lies in a range of numbers:

#part of the tested class
class Item
  def initialize(value = 70 + rand(30))
    @value = value
  end

I tried the assertions in minitest assertions list but they didn't work. I solved the problem by using assert_in_delta as shown below:

#test_value.rb
class ValueTestCase < Test::Unit::TestCase
  def test_if_value_in_range
    item = Item.new
    assert_in_delta(85, item.value, 15)
  end
end

But would like to know if there is a formal assertion for this.

2012-04-04 17:26
by barerd


5

Another way would be to use Range#include?:

assert_includes 70..100, p.value
2012-04-04 17:39
by Niklas B.
@barerd: The API docs are at http://docs.seattlerb.org/minitest/. assert_includes is defined in Minitest::Assertions, but I can't find assert_contains. The link in your question also lists assert_includes - Niklas B. 2012-04-04 18:04
Well, http://docs.seattlerb.org/ is a certainly better doc the one I found. Thank you for that - barerd 2012-04-05 05:01
I prefer this solution because it gives a more useful failure messag - ReggieB 2014-12-15 09:49


8

assert(item.value.between?(70, 100))
2012-04-04 17:48
by steenslag
This is better than Niklas's answer as it works on floats as wel - hammady 2014-02-19 09:22


0

assert((min..max) === result)

Works with Test::Unit.

2018-07-25 12:03
by Leam
Ads