Saturday, February 18, 2012

MS UnitTestFramework: CollectionAssert.AreEquivalent Failing for 2 identical lists

I was doing assert for 2 lists with MS unit test framework, I knew the 2 lists were the same, have the same elements, but strangely it was failing with the below error
Failed    GetCombinationsTest    MyUnitTesting    CollectionAssert.AreEquivalent failed. The expected collection contains 1 occurrence(s) of <System.Int32[]>. The actual collection contains 0 occurrence(s).     
I was very surprised, I tried to look form something wrong I did, couldn’t find any
Lastly I wanted to test the AreEquivalent method
So I have initialized the expected and the actual  the same way

List<Int32[]> expected = new List<Int32[]>(); expected.Add(new Int32[] { 1 }); expected.Add(new Int32[] { 2 }); List<Int32[]> actual = new List<Int32[]>(); actual.Add(new Int32[] { 1 }); actual.Add(new Int32[] { 2 }); //actual = target.GetCombinations(); Assert.AreEqual(expected.Count, actual.Count); CollectionAssert.AreEquivalent(expected, actual);
By this I am pretty sure that they are the same, running the test again, still it was failing with the same error

So there must be something wrong with CollectionAssert.AreEquivalent
I have tried also AreEqual method
It was giving another error
Failed    GetCombinationsTest    MyUnitTesting    CollectionAssert.AreEqual failed. (Element at index 0 do not match.)   

I thought to give Nunit a shot with the same method, it was passing
I have tried with MS doing collection for list of int, like below, and it was passing

            List<int> expected = new List<int>(new int[]{1,2,3});
            List<int> actual = new List<int>(new int[] { 1, 2, 3 });
            CollectionAssert.AreEqual(expected, actual);

So while doing assertion for simple list of int, it is passing, but for list of array if integers, somehow it is not passing for the same lists