流利的断言:大约比较两个数字集合 [英] Fluent Assertions: Compare two numeric collections approximately

查看:71
本文介绍了流利的断言:大约比较两个数字集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个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.

这篇关于流利的断言:大约比较两个数字集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆