Fluentassertions: Add `async`-support for `Where()`

Created on 15 Jul 2020  路  4Comments  路  Source: fluentassertions/fluentassertions

It looks like there is no counterpart of Where method to check exception properties for async delegates.

Would be nice to have an option to check exception properties:

await foo.Awaiting(_ => _.AsyncMethodThroingException())
                .Should().ThrowAsync<Exception>().Where(ex => ...);
enhancement help wanted

Most helpful comment

As ThrowAsync returns a Task<ExceptionAssertions<TException>> you have to await the task before calling Where as . binds stronger than await.

(await foo.Awaiting(_ => _.AsyncMethodThroingException())
    .Should().ThrowAsync<Exception>())
    .Where(ex => ...);

We got an this extension method for WithMessage in #1055 to avoid the extra parentheses
https://github.com/fluentassertions/fluentassertions/blob/c192ca014ae920c96f76a747b72bf110c31ba153/Src/FluentAssertions/AssertionExtensions.cs#L791-L799

and I see no reason why we couldn't also have similar extension methods for the remaining three methods on ExceptionAssertions<TException>

  • WithInnerException
  • WithInnerExceptionExactly
  • Where

Did I mention that we welcome contributions? :wink:

All 4 comments

As ThrowAsync returns a Task<ExceptionAssertions<TException>> you have to await the task before calling Where as . binds stronger than await.

(await foo.Awaiting(_ => _.AsyncMethodThroingException())
    .Should().ThrowAsync<Exception>())
    .Where(ex => ...);

We got an this extension method for WithMessage in #1055 to avoid the extra parentheses
https://github.com/fluentassertions/fluentassertions/blob/c192ca014ae920c96f76a747b72bf110c31ba153/Src/FluentAssertions/AssertionExtensions.cs#L791-L799

and I see no reason why we couldn't also have similar extension methods for the remaining three methods on ExceptionAssertions<TException>

  • WithInnerException
  • WithInnerExceptionExactly
  • Where

Did I mention that we welcome contributions? :wink:

and I see no reason why we couldn't also have similar extension methods for the remaining three methods

In #1324 I already tried to create an extension for WithInnerException. The result is not as good as expected because it requires additional generic parameter. Hope, somebody will find better solution for a compact fluent API.

The extension for Where should not cause such problem and should be added.

Yet there is a small syntactic issue with WithInnerException and WithInnerExceptionExactly:

They are parameterized explicitly with TInnerExcpetion and implicitly from the class with TException. Hence, extension method must specify two generic type arguments, and despite C# will inference the type of TException from this parameter, it will force the user to write the type anyway =(

await foo.Awaiting(_ => _.Bar()).ThrowsAsync<MyException>().WithInnerException<MyException, MyInnerException>(...);

Maybe it's worth changing the API here a bit, and add: ThrowsWithInnerExceptionAsync<TException, TInnerException>()

What is your opinion?

As ThrowsWithInnerExceptionAsync asserts more than one thing at a time, my initial gut feeling is that it moves in a direction we've tried to avoid in Fluent Assertions.
(I hope I'm not contradicting anything I've said earlier 馃).

A headache for both implementers and users would e.g. be which of the two the return type should be.

Task<ExceptionAssertions<TException>>
Task<ExceptionAssertions<TInnerException>>

(See e.g. #433 for the discussions in preparing V5)

To keep the test more readable, I would simply wrap the line.

await foo.Awaiting(_ => _.Bar()).ThrowsAsync<MyException>()
    .WithInnerException<MyException, MyInnerException>(...);

Then in a future with some partial type inference syntax, I'll (maybe) let an analyzer rewrite it to

await foo.Awaiting(_ => _.Bar()).ThrowsAsync<MyException>()
    .WithInnerException<, MyInnerException>(...);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

shift-evgeny picture shift-evgeny  路  5Comments

veleek picture veleek  路  3Comments

infosaurus picture infosaurus  路  4Comments

julealgon picture julealgon  路  3Comments

dennisdoomen picture dennisdoomen  路  3Comments