Skip to content

Add equivalent to xUnit's Assert.Collection #882

@nzkeith

Description

@nzkeith

Description

xUnit has this:

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)

Proposed API

Per https://fluentassertions.slack.com/archives/C039Y2H7U/p1527880297000421

things.Should().SatisfyRespectively(thing => { ... }, thing => { ... });

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions