I have a unit test for a method which gets an object from a collection. This keeps failing and I cannot see why, so I have created a very simple test below to create 2 supplier object and test they are equal to see if I can spot the problem in my test of my code. But this test again is failing. Can anyone see or explain why?
[TestMethod()]
public void GetSupplierTest2()
{
Supplier expected = new Supplier();
expected.SupplierID = 32532;
expected.SupplierName = "Test 1"
Supplier actual = new Supplier();
actual.SupplierID = 32532;
actual.SupplierName = "Test 1"
Assert.AreEqual(expected, actual);
}
But if I test the individual properties of the objects the test passes...
[TestMethod()]
public void GetSupplierTest2()
{
Supplier expected = new Supplier();
expected.SupplierID = 32532;
expected.SupplierName = "Test 1"
Supplier actual = new Supplier();
actual.SupplierID = 32532;
actual.SupplierName = "Test 1"
Assert.AreEqual(expected.SupplierID , actual.SupplierID );
Assert.AreEqual(expected.SupplierName , actual.SupplierName );
}
When testing the individual properties, you compare the string/integer values. They are equal, and so the tests pass.
When testing the parent objects, you compare only the two container structures of type Supplier - and even though those may hold equal property values, they are not equal: Since you are instantiating two separate objects, they do not reside at the same address in memory.
As every other answer says the issue is that you're trying to compare instances of
Supplier
[probably] without overridingEquals
method. But I do not think you should overrideEquals
for test purposes since it may affect production code or you may need anotherEquals
logic in production code.Instead you should either assert each member one by one as you do it in first sample (if you do not have a lot of places where you want to compare entire object) or encapsulate this comparison logic in some class and use this class:
// Test code:
The default implementation of
Object.Equals
for reference types (ie. classes) is "Reference Equality": are the two objects actually the same instance. It doesn't compare the values of fields.Either (as others have shown) override
Equals
to give "Value Equality". In this caseyou must also overrideGetHashCode
(so containers work), and should overrideoperator ==
.Alternatively accept that most entities should have reference equality (two suppliers with the same name are not always the same organisation) and actually use the properties directly.
You compare 2 different instances of the Supplier type, that's why Assert fail.
If you want to Supplier are equals say (by their Id) you can override Equals method, here very over simpled example, :D.
If you want to compare two different instances of Supplier, and want them to be considered equal when certain properties have the same value, you have to override the
Equals
method onSupplier
and compare those properties in the method.You can read more about the Equals method here: http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx
Example implementation:
Note that you'll also get a compiler warning that you have to implement GetHashCode as well, that could be as simple as this:
/* With respect to the controversy around accessing private fields in unit tests, I believe it should never be used unless absolutely neccessary (i.e. time and expense management issues). */