Assert.AreEquals calls object.Equals regardless of IEquatable implementation

Today, when I wrote and ran some tests, I found out that although my class implemented the IEquatable<MyClass> , Assert.AreEqual(instanceOfMyClass, anotherInstance); led to false.

It turns out that regardless of what Assert.AreEquals calls object.Equals(object obj) instead of the correct function MyClass.Equals(MyClass obj) .

Assuming there is a logical reason, what is it?

+4
source share
1 answer

You can try overloading == in the class in question and use the generic overload "Assert.AreEqual <T>", which uses the equals operator.

Personally, I would prefer to use Assert.AreEqual(true,InstanceA.Equals(InstanceB)) , as this will definitely use your Equals method; of course, this assumes that you have checked the unit of your Equals method and are 100% sure that it works :)

As for why Assert uses object.Equals, I think we will need to ask the class designer :) But I suppose one of the reasons is that if your Equals method is wrong, your test result is not good.

0
source

Source: https://habr.com/ru/post/1500630/


All Articles