Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add `async`-support for `WithMessage()` #1052

Closed
mu88 opened this issue May 27, 2019 · 4 comments

Comments

Projects
None yet
3 participants
@mu88
Copy link
Contributor

commented May 27, 2019

Description

It would be great to use WithMessage() on ThrowAsync<>().

Complete minimal example reproducing the issue

// Arrange
Func<Task> act = () => throw new InvalidOperationException("Hello World");

// Act & Assert
await act().Should().ThrowAsync<InvalidOperationException>().WithMessage("HelloWorld");

Actual behavior:

Compile-time error. If have to await/block the Task to use WithMessage() (e. g. await act().Should().ThrowAsync<InvalidOperationException>().Result.WithMessage("HelloWorld");).

Versions

5.6.0

@jnyrup

This comment has been minimized.

Copy link
Collaborator

commented May 27, 2019

Currently you need to await the ThrowAsync before calling WithMessage.

(await act().Should().ThrowAsync<InvalidOperationException>()).WithMessage("HelloWorld");

note the extra parentheses.

To avoid those parentheses you could create an extension method like this

public static async Task<ExceptionAssertions<TException>> WithMessage<TException>(
    this Task<ExceptionAssertions<TException>> task,
    string expectedWildcardPattern,
    string because = "",
    params object[] becauseArgs)
    where TException : Exception
{
    return (await task).WithMessage(expectedWildcardPattern, because, becauseArgs);
}

and then call

await act().Should().ThrowAsync<InvalidOperationException>().WithMessage("HelloWorld");
@mu88

This comment has been minimized.

Copy link
Contributor Author

commented May 27, 2019

Works like a charm! Is it okay for you if I provide this extension method as a PR?

@dennisdoomen

This comment has been minimized.

Copy link
Member

commented May 27, 2019

Absolutely!

mu88 added a commit to mu88/fluentassertions that referenced this issue May 28, 2019

mu88 added a commit to mu88/fluentassertions that referenced this issue May 29, 2019

jnyrup added a commit that referenced this issue May 30, 2019

Provide extension method 'WithMessage()' for async situations (#1052) (
…#1055)

* Provide extension method 'WithMessage()' for async situations (#1052)

* Move method 'WithMessage()' away from dedicated partial and add documentation (#1052)

* Split into separate tests (#1052)
@jnyrup

This comment has been minimized.

Copy link
Collaborator

commented May 31, 2019

Contributed in #1055

@jnyrup jnyrup closed this May 31, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.