问题描述
我正在尝试使用C#中的FluentAssertions建立两个列表的等价关系,其中有两点很重要:
I'm trying to establish equivalence of two lists using FluentAssertions in C#, where two things are of importance:
- 将元素通过其持有的值进行比较,而不是通过引用进行比较(即,它们是等效的,而不是相等的)
- 列表中元素的顺序很重要
在FluentAssertions(甚至NUnit)中没有执行此操作的功能吗?
Is there no function in FluentAssertions (or even NUnit) that does this?
干杯!
推荐答案
默认情况下,ShouldBeEquivalentTo()
将忽略集合中的顺序,因为在大多数情况下,如果两个集合以任意顺序包含相同的项目,则它们是等效的.如果您确实在意顺序,只需在options =>
参数上使用WithStrictOrdering()
的重载之一.
By default, ShouldBeEquivalentTo()
will ignore the order in the collections because in most cases two collections are equivalent if they contain the same items in any order. If you do care about the order, just use one of the overloads of WithStrictOrdering()
on the options =>
parameter.
示例:
var myList = Enumerable.Range(1, 5);
var expected = new[]
{
1, 2, 3, 4, 5
};
//succeeds
myList.ShouldBeEquivalentTo(expected, options => options.WithStrictOrdering());
//fails
myList.Reverse().ShouldBeEquivalentTo(expected, options => options.WithStrictOrdering());
在文档中了解有关这些选项的更多信息.
Read more about these options in the documentation.
这篇关于FluentAssertions:排序列表的等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!