我正在尝试使用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#中还有其他断言可用吗?
答案 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);