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

New diagnostic: Consider using Assert.ThatAsync instead of Assert.That #759

Open
Bartleby2718 opened this issue Jun 9, 2024 · 1 comment

Comments

@Bartleby2718
Copy link
Contributor

Bartleby2718 commented Jun 9, 2024

Arguably, one of the biggest advantages of using NUnit 4 is: Proper async/await, you can now await the Asserts. However, it appears that NUnit.Analyzers doesn't have an analyzer or a codefix for that.

Consider the following sync-over-async tests:

[Test]
public async Task Before()
{
    Assert.That(await GetFourtyTwoAsync(), Is.EqualTo(42));
    Assert.That(await GetFourtyTwoAsync("hello world"), Is.EqualTo(42));
}

private static Task<int> GetFourtyTwoAsync() => Task.FromResult(42);

private static Task<int> GetFourtyTwoAsync(string stringToPrintBeforehand)
{
    Console.WriteLine(stringToPrintBeforehand);
    return Task.FromResult(42);
}

It'd be great if these could be fixed to:

[Test]
public async Task After()
{
    await Assert.ThatAsync(() => GetFourtyTwoAsync(), Is.EqualTo(42)); // the first argument could be simplified to just GetFourtyTwoAsync
    await Assert.ThatAsync(() => GetFourtyTwoAsync("hello world"), Is.EqualTo(42));
}

Personally, I think it'd be neat if we could bundle all NUnit 4-related changes to NUnit4XXX. Thoughts? (It does raise the question of whether we should reserve 5XXX or above as well... 🤷)

@manfred-brands
Copy link
Member

The original test more likely looks like:

[Test]
public void Before()
{
    Assert.That(await GetFourtyTwoAsync(), Is.EqualTo(42));
    Assert.That(await GetFourtyTwoAsync("hello world"), Is.EqualTo(42));
}

So any CodeFix would also possibly have to change the method declaration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants