Fluent Assertions: Compare two numeric collections approximately

纵然是瞬间 提交于 2019-12-01 17:18:26

问题


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.

Example:

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);   

Is there an alternative approach?


回答1:


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.




回答2:


I know it's preferable to compare the list but you could iterate it and compare them individually. I can't test the code right now but the following should work...

double[] source = { 10.01, 8.01, 6.01 };
double[] target = { 10.0, 8.0, 6.0  };

for(var i=0; i<source.Length; i++)
    target[i].Should().BeApproximately(source[i], 0.01)


来源:https://stackoverflow.com/questions/17054636/fluent-assertions-compare-two-numeric-collections-approximately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!