How do I exclude a property of all items in IEnumerable when using ShouldBeEquivalentTo?
Asked Answered
S

1

8

In my NUnit/FluentAssertions tests I compare the complex object returned from my system with a reference one using the following code:

    response.ShouldBeEquivalentTo(reference, o => o.Excluding(x => x.OrderStatus)
                                               .Excluding(x => x.Id)
                                               .Excluding(x => x.Items[0].Name)
                                               .Excluding(x => x.Items[0].Article)
                                               .Excluding(x => x.ResponseStatus));

However, this is not exactly what I intended. I'd like to exclude Name and Article for every object in Items list and not only for the 0th. How do I implement this scenario?

I've looked through the documentation and din't find the solution. Am I missing something?

Sulfide answered 12/3, 2013 at 12:42 Comment(0)
J
10

There's an overload of Excluding() that provides an ISubjectInfo that you can use for more advanced selection criteria. With that overload, you can do stuff like:

subject.ShouldBeEquivalentTo(expected, config =>
                config.Excluding(ctx => ctx.PropertyPath == "Level.Level.Text"));
Jiggerypokery answered 12/3, 2013 at 18:54 Comment(4)
How exactly should I use PropertyPath in my example? I've tried PropertyPath == "Items.Name" and "Order.Items.Name" but it doesn't work.Sulfide
What about ctx.PropertyPath.EndsWith("].Name") || ctx.PropertyPath.EndsWith("].Article")?Jiggerypokery
That works, thank you. I guess, I should use a regex if I want to match a collection name as well. Is there any documentation anywhere on how exactly will PropertyPath look like in various circumstances?Sulfide
It looks exactly like what you would expect. E.g. Items[0].Name. You can also look at the EquivalencySpecs in the original source code on CodePlex. We practice TDD, so hopefully you can use them as a kind of API documentation.Jiggerypokery

© 2022 - 2024 — McMap. All rights reserved.