So you finished your tests and green lights all over..good feeling, however how sure are you that your testscases tests what you think they test? And if they indeed test what you believe they tests did your tests test what they should have tested? ..and ok you get the point right ;-)
So the key in understanding this is twofold. Understanding the underlying OO concepts and understanding how this is utilized by Nunits Assertion class.
So first when you compare object to each other you can use different approaches like Equals and ==.
apple.Equals(banana);
apple == banana;
Starting with equals. The normal scenario where not methods are overriden will actually mean that apple.Equals(banana) will equate to (no pun intended) object.Equals(object obj) which in effect means that its the object reference that will be compared.
Now lets say that apple have overriden its equals method. this means that its now up to the apple to decide how to compare with others. This is essentially was Assertion.AssertEquals does. Will check for reference equality or if overriden take that specific types equal method. That is fairly straight forward. Just remember you cannot be gauranteed what you actually compare unless you dig into the code.
Using == again in most cases will compare object references. However if you in apple create your own == operator and you use assertion.Assert(apple,apple1) to check this objects you get a interesting result. Lets say apple's == method compare the age of a apple.
apple1 = apple2;
apple2.Age += 5;
//Test
Assertion.AssertEquals(apple1,apple2);
This should return false, however returns true!
Reason is that the assertEquals(object obj, object obj1) will end up in this call.
public static bool Equals(object objA, object objB)
{
if (objA == objB)
{
return true;
}
if ((objA == null) || (objB == null))
{
return false;
}
return objA.Equals(objB);
}
Remember that we created our own == method so we would expect that method to be called here..however it is not called. The == method we created will only be called when the specific type is "apple". As you can see the equals call casts the apple to type object.
So the key here is to know that operators can never be overriden, however equals can. So if we instead of creating the == method created a Equals method our test will give us the result you would expect. Hope this made sense to you, and I should have made a code sample :-)
So the conclusion here is to understand exactly how the assertions work in Nunit as this is your last chance to ensure that your code tests the things its supposed to. If you can't trust your tests..well then all hope is gone and bugs will start biting :-)