0

I want to know, when an object is equal or not equal to another object with the method Assert.AreEqual.

I have to objects, which are tested false. But when I test every single property with AreEqual the tests are successfull.

That would mean equality does not only depend on properties. Could someone explain that?

(It's a theoretical question, I cannot provide any data)

Clemens
  • 123,504
  • 12
  • 155
  • 268
D.Weder
  • 187
  • 1
  • 7
  • 2
    The default for equality is based on the reference for the objects. So `new object().Equals(new object())` is `false`. – Enigmativity Feb 04 '19 at 11:44
  • Can you share what kind of objects you are comparing? – Chetan Feb 04 '19 at 11:44
  • 3
    Only because two objects have the same properties doesn´t make them "equal". You have to provide some logic under whitch circumstences you consider two objects equal. There´s no automatism (except that one for reference-equality) here. – MakePeaceGreatAgain Feb 04 '19 at 11:46
  • It also may depend on the unit-testing framework you use. For NUnit it's documented here: https://github.com/nunit/docs/wiki/EqualConstraint – Klaus Gütter Feb 04 '19 at 11:50
  • Or in other words: only because you have two persons named "John" with same age, they are not neccessarily the same person, are they? – MakePeaceGreatAgain Feb 04 '19 at 11:55
  • You could write your own custom comparison logic that uses reflection to compare all property values, and handles property recursion with whatever rules you want. – Bradley Uffner Feb 04 '19 at 12:53

0 Answers0