XUnit Assertion for checking equality of objects

You need to have a custom comparer to achieve this, when you compare objects otherwise they are checked on the basis of whether they are referring to the same object in memory. To override this behavior you need to override the Equals and GetHashCode method and then you could do:

Assert.True(obj1.Equals(obj2));

Here is an MSDN page abt overloading Equals method: http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

Also apt the comment on the question: What's the difference between IEquatable and just overriding Object.Equals()?


FluentAssertions library has some pretty powerful comparison logic inside.

myObject.ShouldBeEquivalentTo(new { SomeProperty = "abc", SomeOtherProperty = 23 });

You can even use this to assert on part of "myObject". However, it might not help you with the private fields.


I had similar issue, but then luckily I am already using

using Newtonsoft.Json;

So I just had to serialize it to json object then compare as string.

var obj1Str = JsonConvert.SerializeObject(obj1);
var obj2Str = JsonConvert.SerializeObject(obj2);
Assert.Equal(obj1Str, obj2Str );