Skip to content

Shorter syntax for exception assertions #1015

@ejball

Description

@ejball

I'd like to be able to perform exception assertions in a single statement, without using the subject.Invoking(...) pattern (e.g. for calling static methods), and without having to create an Action variable first.

I propose a static class with Invoking, Awaiting, and Enumerating methods for this purpose, e.g.:

public static class FluentAction
{
    public static Action Invoking(Action action) => action;
    public static Func<T> Invoking<T>(Func<T> func) => func;
    public static Func<Task> Awaiting(Func<Task> action) => action;
    public static Func<Task<T>> Awaiting<T>(Func<Task<T>> func) => func;
    public static Action Enumerating(Func<IEnumerable> enumerable) => enumerable.Enumerating();
    public static Action Enumerating<T>(Func<IEnumerable<T>> enumerable) => enumerable.Enumerating();
}

Used like so:

FluentAction.Invoking(() => MyClass.Create(null)).Should().Throw<ArgumentNullException();

Or even:

using static FluentAssertions.FluentAction;

...

Invoking(() => MyClass.Create(null)).Should().Throw<ArgumentNullException();

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