TUnit icon indicating copy to clipboard operation
TUnit copied to clipboard

Unnecessary and prohibiting error about `await using` on `Assert.Multiple()`

Open Rekkonnect opened this issue 1 year ago • 4 comments

I'm using the version 0.1.790, which only contains the single Assert.Multiple() overload that can be used as a scope without writing all the assertion logic inside a lambda expression passed into the multiple assertion scope.

There is an analyzer that reports the following error:

  • TUnitAssertions0004 - Assert.Multiple needs an await using before it.

Which I find unnecessary. If you don't include the await keyword, the C# compiler itself will report an error about applying a simple using on an IAsyncDisposable, hinting using await using.

Example screenshot:

image

Workaround

To use this you need to suppress the error by doing:

#pragma warning disable TUnitAssertions0004 // Assert.Multiple needs an `await using` before it

VS provides a quick fix for that by suppressing and then restoring the error, although I would recommend completely disabling it throughout the entire file, or in the .editorconfig entirely.

Rekkonnect avatar Sep 21 '24 18:09 Rekkonnect

Thanks - I'll look into this. Current workaround (apart from the pragma disable) is using a scope for the using:

await using (Assert.Multiple())
{
    ...
}

thomhurst avatar Sep 21 '24 18:09 thomhurst

Imo it's best to completely remove this error, for the reason I explained above. I prefer typing the extra var _ = to allow the inline syntax, and avoid nesting all my assertions within the scope, since most of my testing logic is very small and linear.

Rekkonnect avatar Sep 21 '24 19:09 Rekkonnect

I'll just make the analyzer accept both versions. I won't remove it because the dispose call is what performs the assertions, so if there's no analyzer to remind people to dispose then their assertions wouldn't run

thomhurst avatar Sep 21 '24 19:09 thomhurst

Oh I completely missed the point of this diagnostic, thanks for the explanation

Rekkonnect avatar Sep 21 '24 19:09 Rekkonnect

@Rekkonnect fixed in 0.1.797

thomhurst avatar Sep 22 '24 18:09 thomhurst