Recently I was trying to write a unit test testing to see if one object was equal to another. The objects themselves were simple, just a collection of built-in types, so it should be easy to test.
.Equals
However, since I didn’t implement .Equals, C# doesn’t natively know how to check if the objects are equal. This isn’t a bad thing – C# just wants you to define what makes an instance of an object ‘equal’ to another one.
Message: Assert.Equal() Failure
Expected: SimpleObject { Id = 42, Name = “George” }
Actual: SimpleObject { Id = 42, Name = “George” }
For my case, I just wanted to check each public property and see if they are the same values on both instances.
Enter Reflection
Rather than write a function to check each property individually (like you probably would do in a .Equals implementation), Reflection (also known as black magic, voodoo, or pixie dust) can loop over the public properties and check them for us.
I added this to my testing base class so that I can just easily pass two objects to it and have it do all the assertions for me.
Disclaimer: I haven’t written unit tests specifically against this to check to make sure it works in all cases, so I’m not guaranteeing anything. It was just something I wrote up quick and thought it would be useful for myself in the future, so I documented it here. I tried to put a couple safeguards for the low-hanging fruit in there, but, ultimately, the code is provided here with no warranty.