简体   繁体   English

流利的断言和List的比较 <List<string> &gt;

[英]Fluent assertions and comparison of List<List<string>>

I have a problem using Fluent Assertions to compare two collections of type List<List<string>> . 我在使用Fluent断言来比较List<List<string>>类型的两个集合时遇到问题。 When using the Should().Equal() method (order is important) I get the following (cryptic ;-) message: 当使用Should().Equal()方法(顺序很重要)时,我收到以下(隐含;-)消息:

Expected collection to be equal to {{"Telefoonnummer"}, {"E-mailadres"}, {"E-mailadres controle"}}, but {{"Telefoonnummer"}, {"E-mailadres"}, {"E-mailadres controle"}} differs at index 0.

So, the objects appear to be equal. 因此,对象看起来是相等的。 Also when debugging the objects appear to be exactly the same. 同样,在调试时,对象似乎完全相同。 When comparing two List<string> objects the test passes, no problems, but the same test with List<List<string>> fails. 比较两个List<string>对象时,测试通过,没有问题,但是使用List<List<string>>的相同测试失败。 Am I using the wrong assertion method? 我使用了错误的断言方法吗? Or does Fluent Assertions not handle this type of collection correctly? 还是Fluent断言不能正确处理这种类型的收集?

While comparing a string using == checks value equality, List<string> checks the address of the list. 当使用==比较string将检查值的相等性,而List<string>检查列表的地址。 This means two lists, containing the same elements are not the same because you are comparing the addresses of the lists instead of the items inside. 这意味着包含相同元素的两个列表不相同,因为您正在比较列表的地址而不是内部的项目。 Lets make an example: 让我们举个例子:

List<string> listA = new List<string> { "item" };
List<string> listB = new List<string> { "item" };
bool equal = listA == listB; //equal will be false

To solve your problem you could combine SelectMany and SequenceEqual to compare the items inside the lists. 为了解决您的问题,您可以结合使用SelectManySequenceEqual来比较列表中的项目。 Here is a small example: 这是一个小例子:

List<List<string>> listToCompare = new List<List<string>>()
{
    new List<string> {"first", "second"},
    new List<string> {"third"}
};
List<string> expectedList = new List<string> { "first", "second", "third" };
bool sequenceEqual = listToCompare.SelectMany(i => i).SequenceEqual(expectedList); //sequenceEqual will be true

Instead of Should().Equal() use actualList.ShouldBeEquivalentTo(expectedList. config => config.WithStrictOrder()); 而不是Should().Equal()使用actualList.ShouldBeEquivalentTo(expectedList. config => config.WithStrictOrder());

ShouldBeEquivalentTo method will check that both lists contains items with same values. ShouldBeEquivalentTo方法将检查两个列表是否包含具有相同值的项目。 In cases when list contains instance of reference types, it will compare the full object graph of those instances. 如果list包含引用类型的实例,它将比较那些实例的完整对象图。 Adding as a second parameter 添加为第二个参数
config => config.WithStrictOrdering() will check that order of items is same as in expected list. config => config.WithStrictOrdering()将检查项目顺序与期望列表中的顺序相同。

Should().Equal() on other hand will use "standard" equality check, where var listOne = new List<string> { "test" }; 另一方面, Should().Equal()将使用“标准”相等性检查,其中var listOne = new List<string> { "test" }; and var listTwo = new List<string> { "test" }; var listTwo = new List<string> { "test" }; will be different instances. 将是不同的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM