CollectionAssert无法完全匹配-需要最佳方法

时间:2018-10-29 06:39:12

标签: c# list linq mstest assertion

我正在尝试使用CollectionAssert比较两个列表,但是比较完全匹配失败,并且也没有告诉哪个值不正确

List<string> ExpectedList = new List<string>() { "apple","orange","grapes","mango"};
List<string> ActualList = new List<string>() { "gova","orange","GRAP"};
CollectionAssert.AreEqual(ExpectedList, ActualList)

预期结果应为字符串

  

“苹果gova,葡萄GRAP,空芒果”

我该如何更高效或更简单地进行操作? C#中还有其他断言可用吗?

1 个答案:

答案 0 :(得分:3)

使用Zip这样的方法:

List<string> ExpectedList = new List<string>() {"apple", "orange", "grapes", "mango"};
List<string> ActualList = new List<string>() {"gova", "orange", "GRAP"};

var result = ExpectedList.Zip(ActualList, (first,second) => first != second ?
        $"Mismatch = {first} , {second}" :  "")
            .Concat(ExpectedList.Skip(ActualList.Count))
            .Concat(ActualList.Skip(ExpectedList.Count))
            .Where(c=>!string.IsNullOrWhiteSpace(c)).ToList();

如果要以字符串形式获取结果:

string theStringVersion = string.Join(",", result);