Skip to content

[API Proposal]: Add Should().Throw() and ThrowAsync() without the need to specify a specific exception type #3056

@dennisdoomen

Description

@dennisdoomen

Background and motivation

In some cases, I don't care about the specific exception type that should be thrown. I just want to verify that an exception was thrown.

API Proposal

public class DelegateAssertions<TDelegate, TAssertions>
{
        public ExceptionAssertions<Exception> Throw([StringSyntax("CompositeFormat")] string because = "", params object[] becauseArgs) 
}

public class AsyncFunctionAssertions<TTask, TAssertions>
{
    public async Task<ExceptionAssertions<Exception>> ThrowAsync(
        [StringSyntax("CompositeFormat")] string because = "", params object[] becauseArgs)
}

API Usage

var act = () => Foo();
act.Should().Throw().WithMessage("*SomeMessage*");

var act = () => FooAsync();
await act.Should().ThrowAsync().WithMessage("*SomeMessage*");

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved, it can be implemented

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions