I’ve been looking for a way to check two objects, but I find Equals, Assert,AreEqual and some other ones and I do not know which one I should use. Furthermore, I’ve been making my tests with the new Nunit syntax and I have a hard time finding the new syntax for comparing two objects. Here’s my test and my code:
[Test]
public void CheckForDriversSelectedMoreThanOnce_ReturnsDriversSelectedMoreThanOnceAndTheirSelectedPositions()
{
//arrange
Prediction prediction = new Prediction();
Driver expected = new Driver { position = 1, name = "Michael Schumacher" };
//act
var actual = prediction.CheckForDriversSelectedMoreThanOnce();
//assert
//Assert.That(actual, Is.EqualTo(expected));
//Assert.That(Is.Equals(actual, expected);
Assert.AreEqual(expected, actual);
}
public Driver CheckForDriversSelectedMoreThanOnce()
{
Driver driver = new Driver { position = 1, name = "Michael Schumacher" };
return driver;
}
The line Assert.That(actual, Is.EqualTo(expected)); and Assert.AreEqual(expected, actual); gives me
Expected:
But was:
The other line Assert.That(Is.Equals(actual, expected); gives me:
Expected: True
But was: False
You may need to implement IEquatable within your Class, similiar to;
I had this problem occur when attempting to compare 2 objects in a linq statement using ‘Contains’ and the above resolved that.
I am assuming that Assert.AreEqual(obj1, obj2) at some point does obj1.Equals(obj2), in which case they will not match.
Another option would be to Assert the actual properties, rather than matching the objects