'why is the unit test not passed?
I have such a test:
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
List<int> gameboard = new List<int> {
-1,0,-1,0,-1,0,-1,0,
0,-1,0,-1,0,-1,0,-1,
-1,0,-1,0,-1,0,-1,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1
};
int x = 5;
int y = 1;
List<List<int>> expected = new List<List<int>> {
new List<int>(){
-1,0,-1,0,-1,0,-1,0,
0,-1,0,-1,0,-1,0,-1,
-1,0,-1,0,-1,0,-1,0,
0,0,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,
0,0,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1
}
};
var calc = new Checkers.PieceMovment();
List<List<int>> actual = calc.MoveRightForward(gameboard, x, y);
Assert.AreEqual(expected, actual);
}
}
}
In expected, I enter what I previously received in the console. Because I know it's the right value. And i get this message:
failed Assert.AreEqual. Expected: <System.Collections.Generic.List1[System.Collections.Generic.List1[System.Int32]]>. Actually: <System.Collections.Generic.List1[System.Collections.Generic.List1[System.Int32]]>.
And I don't understand what the problem is . . .
Solution 1:[1]
When you are using collection, use CollectionAssert
Assert.AreEqual(List<int>, List<int>) checks the reference of list 1 and list 2, so it returns false in your case where lists have different references.
CollectionAssert enumerates each item and asserts if they are equal or not.
TL/DR: Use the following code.
CollectionAssert.AreEqual(expected, actual);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sanjay |
