I am using the DeepEqual library, to test if the results of a test match my expected output.
I do the comparison simply as
results.ShouldDeepEqual(expected);
However, I don't know how to ignore a property within my list type
The type I am deep comparing contains a list. The data type held within this list contains a Guid property Id which I want to ignore, as well as a date.
Ignoring top level properties works fine. However, I don't see how to ignore properties on a list type.
To get around this for now, I have had to write some code to clear these properties myself, but this is obviously not ideal.
for (var i = 0; i < results.MyList.Count; i++)
{
results.MyList[i].Id = Guid.Empty;
expectedResults.MyList[i].Id = Guid.Empty;
}
How can I accomplish this?
Having a look through the source code of DeepEqual, the library allows for custom comparisons via the
IComparisoninterface. With that, I simulated the models that you require:Then I went on to creating a custom
IComparisonclass:Here are both of my passing unit tests:
Notes This comparison should ignore the
Idproperty, but am unsure if this is the best way to accomplish the task. There's an IgnoreProperty method which might be better suited for the task, but couldn't see a way to get it to work currently.If anyone has more experience then I do with the library, please let me know of better ways, and I'll update my answer accordingly.