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 suppl
As every other answer says the issue is that you're trying to compare instances of Supplier
[probably] without overriding Equals
method. But I do not think you should override Equals
for test purposes since it may affect production code or you may need another Equals
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:
static class SupplierAllFieldsComparer
{
public static void AssertAreEqual(Supplier expected, Supplier actual)
{
Assert.AreEqual(expected.SupplierID , actual.SupplierID );
Assert.AreEqual(expected.SupplierName , actual.SupplierName );
}
}
// Test code:
SupplierAllFieldsComparer.AssertAreEqual(expected, actual);