DeepEqual icon indicating copy to clipboard operation
DeepEqual copied to clipboard

Ignore property with in list type of sub object with Deep Equal

Open paulinuk opened this issue 5 years ago • 2 comments

Hello

I am using the DeepEqual 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?

paulinuk avatar Nov 08 '20 21:11 paulinuk

I've got the same question too, otherwise this is helping massively. Great library

the-dext avatar Mar 04 '21 11:03 the-dext

results.WithDeepEqual(expected).IgnoreProperty(reader => reader.Name == nameof(ListItemType.Property) && reader.DeclaringType == typeof(ListItemType)).Compare()

markdakers avatar Mar 11 '22 04:03 markdakers

Yes, as @markdakers says, IgnoreProperty takes a predicate which allows you to ignore any property on any type.

For clarity:

results.WithDeepEqual(expected)
    .IgnoreProperty(p => p.DeclaringType == typeof(MyType) && p.Name == nameof(MyType.Id))
    .IgnoreProperty(p => p.DeclaringType == typeof(MyType) && p.Name == nameof(MyType.Date))
    .Assert()

jamesfoster avatar Aug 20 '22 13:08 jamesfoster