流利的断言:大约比较两个数字集合
问题描述:
我有两个double数组.是否可以使用FluentAssertions通过.Be近似()技术来逐个数组比较数组?
I have two arrays of double. Is there a way using FluentAssertions to compare the arrays element-by-element, using the .BeApproximately() technique?
一个范围值足以满足整个数组的需求.
One range value would suffice for the entire array.
示例:
double[] source = { 10.01, 8.01, 6.01 };
double[] target = { 10.0, 8.0, 6.0 };
// THE FOLLOWING IS NOT IMPLEMENTED
target.Should().BeApproximately(source, 0.01);
有替代方法吗?
答
通用集合断言有一个重载,它带有一个Func,可用于在比较期间应用任何谓词.这样,您可以执行以下操作:
There's an overload on the generic collection assertions that takes a Func that you can use to apply any predicate during comparison. With that, you could do something like:
source.Should().Equal(target, (left, right) => AreEqualApproximately(left, right, 0.01));
您唯一需要做的就是自己创建该方法.
The only thing you need to do is to create that method yourself.