Unable to compare lists in UnitTests

I need to compare lists as shown in my unit tests:

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.");

But I always get the exception below, how can I overcome this?

[Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException] = {"CollectionAssert.AreEqual failed. The expected response does not match the actual answer. (Item with index 0 does not match.)"}

+4
source share
7 answers

Alternatively, you might consider using the FluentAssertionsUnit Test framework , which is compatible with Microsoft Unit Test.

Then your code will be as follows:

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.");

He will also work with such things:

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);, , Unit Test .

, 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.");
+1

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.

, .

+3

,

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

False. , .

overload, IComparer, .

+2

. , (, < > ).

.

[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

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 , , , .

0

SequenceEqual bool

-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");
 }
-1

All Articles