Search code examples
scalaunit-testingspecs2

How to use spec2 to assert the difference of two integers are less than 1000?


Specs2 test:

val num1: Int = getNum1();
val num2: Int = getNum2();

math.abs(num2 - num1) must beLessThan(1000)

This may work, but is not elegant. Is there some better solution to do it?


Update:

I mean I want to find or create some matcher that I can write with:

num1 must beLessOrMoreThan(num2, 1000)

Solution

  • You can write

    (num2 - num1) must beCloseTo(0 +/- 1000)