【发布时间】:2016-08-30 13:57:45
【问题描述】:
这个问题建立在我之前问过的一个基础之上:
Fluent Assertions: Approximately compare a classes properties
如果我有课,说 Vector3
public class Vector3
{
public double X { get; }
public double Y { get; }
public double Z { get; }
public Vector3(double x, double y, double z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
}
而且组成了两个列表,如何近似比较两个列表中Vector3对象的属性,看是否相同。这就是我目前所拥有的(我使用的是 xUnit 框架,但这应该没什么区别):
public double precision = 1e-5;
[Fact]
public void ApproximatelyCompareVector3List()
{
// Arrange
var expectedList = new List<Vector3>
{
new Vector3(0.5, 1, 3),
new Vector3(0, 2, 4)
};
// Act
var calculatedList = List<Vector3>
{
new Vector3(0.4999999, 1.0000001, 3),
new Vector3(0.0000001, 2.0000001, 4)
};
//Assert
calculatedList.ShouldBeEquivalentTo(expectedList, options => options
.Using<double>(ctx => ctx.Subject.Should().BeApproximately(ctx.Expectation, precision))
.When(info => info.SelectedMemberPath == "X" ||
info.SelectedMemberPath == "Y" ||
info.SelectedMemberPath == "Z" ));
}
但是,这似乎跳过了近似测试并需要精确排序。是否可以有确切的排序或任何排序来近似比较列表中包含的对象的属性?
【问题讨论】:
标签: c# unit-testing fluent-assertions