XUnit Assertion for checking equality of objects

inquisitive picture inquisitive · Jun 21, 2012 · Viewed 35.2k times · Source

I am using XUnit framework to test my C# code.

Is there any assert method available in this framework which does the object comparison? My intention is to check for equality of each of the object's public and private member variables.

I tried those alternatives but seldom it works:

1) bool IsEqual = (Obj1 == Obj2)
2) Assert.Same(Obj1, Obj2) which I couldnt understand what happens internally

Answer

The Integrator picture The Integrator · May 2, 2017

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 );