5

What's the closest FluentAssertions equivalent to the following xUnit/FluentAssertions combo?

Assert.Collection(things,
    thing =>
    {
        thing.Id.Should().Be(guid1);
        thing.Name.Should().Be("Thing1");
        thing.Attributes.Should().NotBeNull();
        thing.FullName.Should().MatchRegex("^Thing1 [^ ]+$");
    },
    thing =>
    {
        thing.Id.Should().Be(guid2);
        thing.Name.Should().Be("Thing2");
        thing.Attributes.Should().NotBeNull();
        thing.FullName.Should().MatchRegex("^Thing2 [^ ]+$");
    });

In some situations I find this style more expressive and/or succinct than the FluentAssertions methods that assert collection equality or equivalence.

Assert.Collection signature:

/// <summary>
/// Verifies that a collection contains exactly a given number of elements, which meet
/// the criteria provided by the element inspectors.
/// </summary>
/// <typeparam name="T">The type of the object to be verified</typeparam>
/// <param name="collection">The collection to be inspected</param>
/// <param name="elementInspectors">The element inspectors, which inspect each element in turn. The
/// total number of element inspectors must exactly match the number of elements in the collection.</param>
public static void Collection<T>(IEnumerable<T> collection, params Action<T>[] elementInspectors)

Related issue: https://github.com/fluentassertions/fluentassertions/issues/118

nzkeith
  • 343
  • 2
  • 10
  • 1
    I think you're mixing concepts here. Those calls to `Should` will throw if the assertion isn't met and I suspect `Assert.Collection` expects a predicate. What are you trying to accomplish with that wrapping? – Dennis Doomen May 23 '18 at 06:43
  • Thanks for responding. I'm pretty sure this is what Assert.Collection is designed for. I've added the Assert.Collection signature to the question to clarify. – nzkeith May 23 '18 at 16:25
  • Ah, so they probably just catch any exceptions that FA would throw. – Dennis Doomen May 24 '18 at 10:59
  • Yep, and if multiple actions throw, the final assertion failure includes all of the individual failures. It's pretty handy. – nzkeith May 24 '18 at 15:54
  • 2
    GitHub issue: https://github.com/fluentassertions/fluentassertions/issues/882 – nzkeith Aug 01 '18 at 17:52

0 Answers0