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

Go To


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

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:

class ValueTestCase < Test::Unit::TestCase
  def test_if_value_in_range
    item =
    assert_in_delta(85, item.value, 15)

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

2012-04-04 17:26
by barerd


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 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, 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


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


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

Works with Test::Unit.

2018-07-25 12:03
by Leam