The Fluent Assertions framework provides a plethora of collection-related assertions.
For checking whether a given collection contains an element that satisfies a specific condition, I can use Should().Contain
:
items.Should().Contain(item => item.Job == Job.Pilot, "because each plane has a pilot");
I can make sure there is only one matching element:
items.Should().ContainSingle(item => item.Job == Job.Pilot, "because each plane has one pilot");
I can also check all items based upon expressions (presumably irrespective of ordering, though the docs are a bit vague about that):
items.Should().SatisfyRespectively(
item => item.Job == Job.Pilot,
item => item.Job == Job.CoPilot,
item => item.Job == Job.FlightAttendant,
item => item.Job == Job.FlightAttendant,
item => item.Job == Job.FlightAttendant,
item => item.Job == Job.FlightAttendant
);
How can I check whether a given number of items (at no particular position in the enumeration) match a given condition, while ignoring the rest?
I'm imagining something like
items.Should().ContainMultiple(4, item => item.Job == Job.FlightAttendant);
or even
items.Should().Times(4).Contain(item => item.Job == Job.FlightAttendant);
but I couldn't find anything in the docs.
I know I can pre-filter my collection with regular System.Linq
functionality, but of course, that is in conflict with the (reasonable) recommendation that
By having Should() as early as possible in the assertion, we are able to include more information in the failure messages.
Is this somehow supported, or should this go into a feature request somewhere?