Today when writing and running some tests, I found out that even though my class implemented an IEquatable<MyClass>
interface, an Assert.AreEqual(instanceOfMyClass, anotherInstance);
resulted in false.
It turns out that no matter what, the Assert.AreEquals
calls object.Equals(object obj)
instead of the correct MyClass.Equals(MyClass obj)
function.
Under the assumption that there is a logical reason, what is it?
You could try overloading == on the class in question and use the generic "Assert.AreEqual< T>" overload which uses the equals operator.
Personally I would prefer to use Assert.AreEqual(true,InstanceA.Equals(InstanceB))
as this is sure to use your Equals method; of course this assumes you have unit-tested your Equals method and are 100% certain that it works :)
As to why Assert uses object.Equals, I guess we'd have to ask the designer of the class :) But I suppose one reason is that if your Equals method is buggy, your test result is no good.
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