无法比较UnitTests中的列表

我需要在unit testing中比较以下列表:

var x = new List() { new List() }; var y = new List() { new List() }; CollectionAssert.AreEqual(x, y, "Expected response not the same as actual response."); 

但我总是得到以下例外,我该如何克服这个?

[Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException] = {“CollectionAssert.AreEqual failed。预期响应与实际响应不同。(索引0处的元素不匹配。)”}

作为替代方案,您可以考虑使用与Microsoft Unit Test兼容的FluentAssertions Unit Test框架。

然后你的代码将成为:

 var x = new List() { new List() }; var y = new List() { new List() }; x.ShouldBeEquivalentTo(y, "Expected response not the same as actual response."); 

它也适用于这种事情:

 var ints1 = new List(); var ints2 = new List(); ints1.Add(1); ints2.Add(1); var x = new List() { ints1 }; var y = new List() { ints2 }; x.ShouldBeEquivalentTo(y, "Expected response not the same as actual response."); 

如果你改变了ints2.Add(1); to ints2.Add(2); ,然后unit testing将正确失败。

请注意, ShouldBeEquivalentTo()递归地下降被比较的对象,并处理集合,因此即使列表列表也可以使用它 – 例如:

 var ints1 = new List(); var ints2 = new List(); ints1.Add(1); ints2.Add(1); // Change this to .Add(2) and the unit test fails. var objList1 = new List { ints1 }; var objList2 = new List { ints2 }; var x = new List { objList1 }; var y = new List { objList2 }; x.ShouldBeEquivalentTo(y, "Expected response not the same as actual response."); 

根据msdn文档。 http://msdn.microsoft.com/en-us/library/ms243736.aspx

如果两个集合在相同的顺序和数量中具有相同的元素,则它们是相等的。 如果元素的值相等,则元素相等,而不是它们引用相同的对象。 默认情况下,使用Equals比较元素的值。

现在看来collections是平等的。 直到你深入了解。 根据文档

在相同的顺序和数量具有相同的元素

从你的例子来看,他们没有相同的元素。 它们具有相同类型的元素,并且这些元素具有相似的签名,但是这两个元素不同。 它们是完全不同的对象。

使用“相同顺序的相同元素”运行测试,看看结果如何。 如。

 List list = new List(); var x = new List() { list }; var y = new List() { list }; CollectionAssert.AreEqual(x, y, "Expected response not the same as actual response."); 

您会发现此传递作为列表,满足CollectionAssert.AreEqual的参数参数。

希望这可以解决它。

这是因为

 new List().Equals(new List()) 

返回False 。 外部列表不相等,因为内部列表不相等。

您可以尝试使用接受IComparer的重载 ,它会将您的两个空列表视为相等。

您应该使用SelectMany提取外部列表的内容,然后检查是否相等,例如:

 var x = new List() { new List() }; var y = new List() { new List() }; var xItems=x.SelectMany(item=>item); var yItems=y.SelectMany(item=>item); CollectionAssert.AreEqual(xItems, yItems, "Expected response not the same as actual response."); 

正如其他人所指出的那样,AreEqual在每个项目上使用Equals来检查是否相等,显然两个不同的List实例永远不会相等。

比较两个空列表的引用,如果需要比较内部值类型,则必须手动比较它(例如,写入List <>扩展名)。

示例扩展。

 [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { var x = new List() { new List(){1} }; var y = new List() { new List(){1} }; x.SequenceRecursiveEqual(y); } } public static class ExtenderListAssert { public static void SequenceRecursiveEqual(this IList sourse, IList expected) { if (sourse.Count != expected.Count) Assert.Fail(); else { for (var i = 0; i < sourse.Count; i++) { var left = sourse[i]; var right = expected[i]; if(left is IList && right is IList) { (left as IList).SequenceRecursiveEqual(right as IList); } else { Assert.AreEqual(left, right); } } } } } 

您可以使用SequenceEqual并检查返回的bool以进行断言

使用此类型:

  [TestMethod] public void AreEqualTest1() { List countries1 = new List { "Israel", "USA", "Germany" }; List countries2 = new List { "Israel", "USA", "Germany" }; // First compare count of both collections:countries1 && countries2 => // if not the same count => test failed. // Otherwise copmare the equality items of both collections in order, // if one of the comparison failed => test failed // otherwise =>=> test passed. CollectionAssert.AreEqual(countries1, countries2, "Not equal, hence failed"); }