Learning how to write unit tests with NUnit.
Struggling to compare two complex objects.
There is an answer to a very similar question here Comparing Two objects using Assert.AreEqual() though it looks like you're expected to override Equals()
on your objects - this isn't ideal given how many objects there could be, that you would like to compare, let alone the number of properties that could exist on the objects and their nested objects.
Given the sample object:
public class AMockObject
{
public int Id { get; set; }
public ICollection<int> Numbers { get; set; }
public AMockObject()
{
Numbers = new List<int>();
}
}
I would like to compare that two separate instances of this object have the same values and am finding Assert.AreEqual()
isn't really doing what I expected.
For example, all of these fail:
// Example 1
AMockObject a = new AMockObject();
AMockObject b = new AMockObject();
Assert.AreEqual(a,b); // Fails - they're not equal
// Example 2
AMockObject a = new AMockObject() { Id = 1 };
AMockObject b = new AMockObject() { Id = 1 };
Assert.AreEqual(a, b); // Also fails
// Example 3
AMockObject a = new AMockObject() { Id = 1 };
a.Numbers.Add(1);
a.Numbers.Add(2);
a.Numbers.Add(3);
AMockObject b = new AMockObject() { Id = 1 };
b.Numbers.Add(1);
b.Numbers.Add(2);
b.Numbers.Add(3);
Assert.AreEqual(a, b); // also fails
We have code in place where we're cloning various objects and some of them a very large. Given this is a pretty common thing to do is there an equally common way to test that two objects are the same at the property-value level?
The example here has two properties. In the real world I have an object with a couple dozen properties, some of which are lists of other complex objects.
For the time being, I am serializing the objects and comparing the strings though this feels less than ideal.