When testing some vector operations in my code I have to check for equality with some tolerance value because the float values may not be an exact match.
Which means that my test asserts are like this:
Assert.That(somevector.EqualWithinTolerance(new Vec3(0f, 1f, 0f)), Is.True);
Instead of this:
Assert.That(somevector, Is.EqualTo(new Vec3(0f, 1f, 0f)));
And that means that my exceptions are like this:
Expected: True
But was: False
Instead of this:
Expected: 0 1 0
But was: 1 0 9,536743E-07
Making it slightly harder to understand what went wrong.
How do I use a custom comparison function and still get a nice exception?
Found the answer. NUnit
EqualConstrainthas a method with the expected name:Using.So I just added this class:
I instantiated it and used it like this:
It’s more typing, but it’s worth it.