Search code examples
scalascalamock

ScalaMock : expect a Double within some tolerance


I am using a MockFunction1, expecting an argument and expecting it to be called once.

mockFunction.expects(result).once()

The type of result object contains several values of type Double.

I want the expected result object to match the actual within some tolerance limits. (For example, if the actual object contains a value 10.742345, I want the expected object with value 10.74 to match).

How can I achieve this?

Edit: The type of result object is of the form:

class Result {
 ...
  m: Metrics,
 ...
}
class Metrics {
 ...
  a: Double,
  b: Double,
 ...

}

Solution

  • Appears there is no built in mechanism in scalamock to give a default comparator for comparing all instances of a type.

    To work around this, I ended up using 'argument capture' and writing a custom comparator for the entire object:

    val actualResult = CaptureOne[Result]()
    mockFunction expects capture(actualResult) once
    ....
    actualResult.value.shouldEqual(result)(EqualityWithTolerance)