Question

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?

Was it helpful?

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.

OTHER TIPS

I came up with this atrocity. Would love to see some better solutions.

var values = new List<Tuple<double, double>>
{
    Tuple.Create(50.0, 0.1),
    Tuple.Create(3.745932, 0.000001),
};
Predicate<double> expected = (x) =>
{
    var value = values[0];
    values.RemoveAt(0);

    Assert.AreEqual(value.Item1, x, value.Item2);
    return true;
};
model.AssertWasCalled(m => m.CalculateBeta(
    Arg<double>.Matches((x) => expected(x)),
    Arg<double>.Matches((x) => expected(x));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top