3

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?

F-H
  • 663
  • 1
  • 10
  • 21
  • 2
    No such API exists unfortunately. – Dennis Doomen Nov 08 '19 at 14:37
  • 1
    @DennisDoomen: This should be an answer, as you are an authoritative source on that topic :) With that said, is there anywhere I can log a feature request for this, to keep the idea around? Maybe some good ideas on how to implement it will turn up and someone ends up contributing it (not excluding myself here, once I've worked a bit more with Fluent Assertions and know more about the library). – F-H Nov 08 '19 at 19:26
  • 1
    @DennisDoomen: Come to think of it, maybe something like `items.Should().ContainSome(item => item.Job == Job.FlightAttendant).Which.Should().HaveCount(4);` would be most in line with the general way Fluent Assertions work. That is, a `ContainSome` method that does basically the same as `Contain`, but returns an `AndWhichConstraint` that returns the filtered collection. Having written my first custom assertions extension for an application-specific type today, I now feel more confident about maybe tackling this at some point in the future. – F-H Nov 13 '19 at 19:10
  • 1
    Yes, what you are suggesting can indeed work, but won't do a deep comparison. – Dennis Doomen Dec 05 '19 at 06:41
  • And you can file a feature request on https://github.com/fluentassertions/fluentassertions/issues – Dennis Doomen Dec 05 '19 at 06:42

0 Answers0