I'm writing an unit tests for ready code and I'm receiving an unexpected AssertFailedException trying to run one of the test. Here is he:
[TestMethod]
public void TestPositionGetter()
{
testPlayer.Position = new int[] { 1, 3 };
int[] expectedPosition = testPlayer.Position;
Assert.AreEqual(expectedPosition, testPlayer.Position);
}
And here is the Position property in the Player class that I'm trying to test:
public int[] Position
{
get
{
return new int[] { this.PositionX, this.PositionY };
}
set
{
this.PositionX = value[0];
this.PositionY = value[1];
}
}
Debugging the test, in local variables window player.Position and expectedPosition are looking similar but the test is still failing. I'm afraid the problem is coming from references.
You are comparing different instances of an int[]
. Assert.AreEqual
compares by reference. Try CollectionAssert.AreEqual
.
CollectionAssert.AreEqual(expectedPosition, testPlayer.Position);
This will compare array elements.
Also, your Position
property smells like bad design. Do you really have to create new array every time you get a value?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With