Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert.Equals of Two objects implementing IEquatable(T) doesn't use the equals method

Tags:

.net

mstest

I have a custom type Type that implement IEquatable(Type). Then I new up two instances of the type, none of them are Null

Assert.IsTrue(obj1.equals(obj2)) //Success
Assert.AreEqual(obj1, obj2) //False
Assert.AreEqual(Type)(obj1, obj2) //False

The first one hits my equals, the second one hits the ToString() Any suggestions?

update
some code to illustrate: http://pastebin.com/1uecrfeW

more update
If I have to override the base equals, even if a better (generic) equals is available, then what's the use of implementing IEquals(T)?

like image 341
Boris Callens Avatar asked Sep 06 '11 13:09

Boris Callens


2 Answers

My guess is that it's actually hitting Equals(object) instead of Equals(T). If you haven't overridden Equals(object) then it's probably failing the assertion, which then uses ToString to create a useful failure message.

If you could show a short but complete program which demonstrates the problem (including which Assert method you're calling - NUnit? Something else?) that would help.

like image 163
Jon Skeet Avatar answered Nov 14 '22 23:11

Jon Skeet


IIRC Assert.AreEqual is non-generic, so only object.Equals applies; try checking the override of non-generic object.Equals.

In addition to the inconvenience of calling a generic method via reflection, the objects could also implement multiple IEquatable<T> (for different T). So the non-generic version makes sense here, IMO.

like image 42
Marc Gravell Avatar answered Nov 15 '22 01:11

Marc Gravell