问题
I love NUnit's constraint-based API. I often use floating point comparison like this:
double d = foo.SomeComputedProperty;
Assert.That(d, Is.EqualTo(42.0).Within(0.001));
Very readable!
However, if I have a custom class whose equality depends on floating point comparison:
class Coord
{
Coord(double radius, double radians)
{
this.Radius = radius;
this.Radians = radians;
}
double Radius { get; }
double Radians { get; }
public override bool Equals(Object obj)
{
Coord c = obj as Coord;
if (obj == null || c == null) return false;
return c.Radians == this.Radians && c.Radius == this.Radius;
}
}
I would like to write my tests like this:
Coord reference = new Coord(1.0, 3.14);
// test another Coord for near-equality to a reference Coord:
Assert.That(testCoord, Is.EqualTo(reference).Within(0.001));
Is it at all possible to use NUnit like this?
来源:https://stackoverflow.com/questions/31752407/how-can-i-use-nunits-equalto-within-constraint-with-a-custom-data-type