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

NUnit2045 code-fix does not correctly lift asynchronous assertions into Assert.Multiple #496

Closed
yaakov-h opened this issue Dec 12, 2022 · 1 comment · Fixed by #498
Closed
Assignees

Comments

@yaakov-h
Copy link

I have NUnit2045 (Call independent assert statements from inside an Assert.Multiple) triggering on the following code:

Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
Assert.That(response.Content.Headers.ContentType?.MediaType, Is.EqualTo("application/json"));
Assert.That(response.Content.Headers.ContentType?.CharSet, Is.EqualTo("utf-8"));
Assert.That(await response.Content.ReadAsStringAsync(), Is.EqualTo(responseBody));

If I apply the code-fix, I am left with the following invalid code:

Assert.Multiple(() =>
{
	Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
	Assert.That(response.Content.Headers.ContentType?.MediaType, Is.EqualTo("application/json"));
	Assert.That(response.Content.Headers.ContentType?.CharSet, Is.EqualTo("utf-8"));
	Assert.That(await response.Content.ReadAsStringAsync(), Is.EqualTo(responseBody));
});

This code binds to Assert.Multiple(TestDelegate) which causes the following error:

CS4034: The 'await' operator can only be used within an async lambda expression. Consider marking this lambda expression with the 'async' modifier.

I would expect that we should use the Assert.Multiple(AsyncTestDelegate) overload by adding async to the lambda, like so:

Assert.Multiple(async () =>
{
	Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
	Assert.That(response.Content.Headers.ContentType?.MediaType, Is.EqualTo("application/json"));
	Assert.That(response.Content.Headers.ContentType?.CharSet, Is.EqualTo("utf-8"));
	Assert.That(await response.Content.ReadAsStringAsync(), Is.EqualTo(responseBody));
});
@manfred-brands
Copy link
Member

@yaakov-h Your conclusion seems to be correct. I'll check if I can detect the await being used.

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

Successfully merging a pull request may close this issue.

3 participants