2013-11-01 95 views
4

我需要比较列表就像在我的单元测试下面的对比列表:不能在单元测试

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

但我总是得到下面的异常,我该如何去克服呢?

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

+0

Similair的问题在这里:http://stackoverflow.com/questions/5194966/mstest-collectionassert-areequivalent-failed-the-expected-collection-containing –

回答

1

作为替代方案,您可以考虑使用FluentAssertions单元测试框架,该框架与Microsoft单元测试兼容。

那么你的代码将变成:

var x = new List<object>() { new List<int>() }; 
var y = new List<object>() { new List<int>() }; 

x.ShouldBeEquivalentTo(y, "Expected response not the same as actual response."); 

这也符合这样的事情工作:

var ints1 = new List<int>(); 
var ints2 = new List<int>(); 

ints1.Add(1); 
ints2.Add(1); 

var x = new List<object>() { ints1 }; 
var y = new List<object>() { ints2 }; 

x.ShouldBeEquivalentTo(y, "Expected response not the same as actual response."); 

如果你改变ints2.Add(1);ints2.Add(2);,单元测试,然后将正确失败。

注意ShouldBeEquivalentTo()递归下降被比较的对象,并处理集合,因此名单中甚至列出将与它的工作 - 例如:

var ints1 = new List<int>(); 
var ints2 = new List<int>(); 

ints1.Add(1); 
ints2.Add(1); // Change this to .Add(2) and the unit test fails. 

var objList1 = new List<object> { ints1 }; 
var objList2 = new List<object> { ints2 }; 

var x = new List<object> { objList1 }; 
var y = new List<object> { objList2 }; 

x.ShouldBeEquivalentTo(y, "Expected response not the same as actual response."); 
0

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

扩展示例。

[TestClass] 
public class UnitTest1 
{ 
    [TestMethod] 
    public void TestMethod1() 
    { 
     var x = new List<object>() { new List<int>(){1} }; 
     var y = new List<object>() { new List<int>(){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); 
       } 
      } 
     } 
    } 
} 
+0

请给我一个代码示例 – user2890243

+0

检查我的重播 – sh1ng

-1

您可以使用SequenceEqual和检查返回布尔的说法

+0

相信这是特定于NUnit的。 OP似乎正在使用Visual Studio测试工具。 –

+0

这些集合中的整数列表不相同,因此SequenceEqual将返回false –

+0

这是linq。 http://msdn.microsoft.com/en-us/library/bb348567.aspx – Epsilon

2

这是因为

new List<int>().Equals(new List<int>()) 

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

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

-1

使用此类型:

[TestMethod] 
public void AreEqualTest1() 
{ 
    List<string> countries1 = new List<string> { "Israel", "USA", "Germany" }; 
    List<string> countries2 = new List<string> { "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"); 
} 
+2

此答案与问题相关? –

3

按照msdn的文档。 http://msdn.microsoft.com/en-us/library/ms243736.aspx

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

现在看来,收集是平等的。直到你深入了解。按照文档

在同一顺序的相同元素和数量

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

使用“相同顺序的相同元素”运行您的测试并查看结果是什么。如。

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

你会发现这个通过列表的参数参数为CollectionAssert.AreEqual满足。

希望这会清除它。

0

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

var x = new List<object>() { new List<int>() }; 
var y = new List<object>() { new List<int>() }; 

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检查平等和明显的两个不同列表实例永远不会相等。