Search code examples
c#floating-pointrhino-mocks

Testing floating point equality with AssertWasCalled


I have a test that ends with this:

model.AssertWasCalled(m => m.CalculateBeta(
    Arg<double>.Is.Equal(50),
    Arg<double>.Is.Equal(3.74593228));

I would like to test this using a delta value like what is provided in Microsoft.VisualStudio.TestTools.UnitTesting.Assert

Assert.AreEqual(3.745932, result, 0.000001);

Is there a way to do this?


Solution

  • Yeah, there is an easier way:

    test.AssertWasCalled(
        t => t.CalculateBeta(
            Arg<double>.Matches(a => Math.Abs(a - 50) < 0.01),
            Arg<double>.Matches(b => Math.Abs(b - 3.74593228) < 0.000001)
        )
    );
    

    Actually, you can pass any predicate to Arg<T>.Matches() to validate each argument particulary.