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

Assert.ThatAsync doesn't seem to work with ValueTask #4588

Open
OsirisTerje opened this issue Dec 13, 2023 · 6 comments
Open

Assert.ThatAsync doesn't seem to work with ValueTask #4588

OsirisTerje opened this issue Dec 13, 2023 · 6 comments

Comments

@OsirisTerje
Copy link
Member

OsirisTerje commented Dec 13, 2023

Twitter user eduardo @ed_dev_ commented about this, and showed the following repro code:

image

See twitter post https://twitter.com/ed_dev_/status/1730951076730323419

Comments @manfred-brands @stevenaw @jnm2 ?

Repro here: https://github.com/nunit/nunit.issues/tree/main/Issue4588

@OsirisTerje OsirisTerje added the Investigate We will look into this label Dec 13, 2023
@eduherminio
Copy link

Adding workaround as well:

await Assert.ThatAsync(() => Method(), Is.EqualTo(""));   // Doesn't work, would work if method returned Task<string>
await Assert.ThatAsync(async () => await Method(), Is.EqualTo(""));  // Works

@OsirisTerje
Copy link
Member Author

Another workaround is

await Assert.ThatAsync(()=>Method().AsTask(),Is.EqualTo(""));

@jnm2
Copy link
Contributor

jnm2 commented Dec 14, 2023

I'm not sure where, but I'd thought we had an existing thread on ValueTask. ValueTask is just one of dozens of awaitable types other than Task. () => Task.Yield() is an example, and so is UWP's IAsyncOperation.

For all of these, async () => await seems quite tolerable. I still think it's good advice to add async/await even when it's not strictly necessary: https://blog.stephencleary.com/2016/12/eliding-async-await.html

With that said, of course ValueTask is much more common as an awaitable method return type than anything else besides Task itself, so maybe we could consider adding extra API overloads just for convenience.

@manfred-brands
Copy link
Member

After re-reading Stephen Toubs article I also think ValueTask will become more in use and a specific overload could be useful. Due to the limitations described in the article it could be that we have to call AsTask() and then call the original API.

@OsirisTerje
Copy link
Member Author

@jnm2 We do have other issues on ValueTask. You authored one back in 2018. I'll link these together.

But this one was very specific, so I thought it was a valid one to keep.

@OsirisTerje
Copy link
Member Author

OsirisTerje commented Dec 27, 2023

@jnm2 Here are all the async issues, open and closed. I have had them collected in a project. https://github.com/nunit/nunit/issues?q=is%3Aissue+project%3Anunit%2Fnunit%2F1+

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

No branches or pull requests

5 participants