semihokur / AsyncFixer

Advanced async/await Diagnostics and CodeFixes for C#
Apache License 2.0
165 stars 11 forks source link

Conflicting AsyncFixer01 on use of FluentAssertions ThrowAsync #33

Closed rcollette closed 1 year ago

rcollette commented 1 year ago

I get a conflicting report that await is not necessary on the last line of this code snippet, but then the compiler complains if I remote the await.

#pragma warning disable AsyncFixer01
    [Fact]
    public async Task CreateUserProfileAsync_WhenNotInBlockListAndActorIdNotAssigned_ThrowsError()
    {
        // Arrange
        CreateUserProfileModel createUserProfileModel = CreateUserProfileModel();
        CreateUserRequestDto createUserRequestDto = new();
        User user = CreateUser();
        _userClient.Setup(s => s.CreateUserAsync(createUserRequestDto)).ReturnsAsync(user);

        // Act
        Func<Task> func =  ()=> _service.CreateUserProfileAsync(createUserProfileModel);

        // Assert
       // Issue is with the following line.
        await func.Should().ThrowExactlyAsync<InvalidOperationException>().WithMessage("The actorId property was not stored on the user profile for user with login user@acme.com ");
    }
#pragma warning restore AsyncFixer01
0liver commented 1 year ago

When you remove the await, you'll also need to remove the async in the method signature.

That works for me.

rcollette commented 1 year ago

.WithMessage is an async function. If I remove the await, and the async from the test method, I get another error.

MA0134: Observe result of async calls
VSTHRD110: Observe the awaitable result of this method call by awaiting it, assigning to a variable, or passing it to another method.

image

0liver commented 1 year ago

How about returning the value (Task) then?

rcollette commented 1 year ago

🤦 So sorry. Part of this is from working in the JavaScript world where having test functions returning promises is generally a nightmare vs. using async/await. I think I automatically default to using async/await on .NET test methods, thinking that the test framework is just going to skip past the test without waiting for the result.

Thanks again.

0liver commented 1 year ago

Happy to help!