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.Throws does not work with async delegates #1190
Comments
Thanks for the report. I have confirmed and assigned this as a high priority issue. Both instances take a TestDelegate, so they should behave the same. As for why Assert.That does not return the exception, it is because we don't know which constraint you are going to use based on the signature. It is common to write fluent asserts like |
Interestingly, this appears to only be a problem with the throws constraint. Other constraints work, [Test]
public void TestTheMeaningOfLife()
{
Assert.That(async () => await DelayedMeaningOfLife(), Is.EqualTo(42));
}
static async Task<int> DelayedMeaningOfLife()
{
await Task.Delay(TimeSpan.FromSeconds(2)).ConfigureAwait(false);
return 42;
} |
What do you mean by a problem with the throws constraint? Unless If |
My mistake, I misread which test was failing and even misread which of my tests was failing. So my comment about the constraint is incorrect. Assert.Throws is one of the few classic asserts that doesn't chain to its equivalent constraint. |
Fixed by #1142, but we will need to document. |
Thanks. I can't see from the changes to #1142, but does the error message ("async void methods are not supported") remain, or does the error indicate that the developer should be using |
@yaakov-h, yes, once we release the next version, Assert.ThrowsAsync should be used instead. For now, use the fluent syntax. |
Stackoverflow brought me here. Great work!! I updated in NuGet to the 3.20 version and could use the Assert.ThrowsAsyc function! |
Just what I was looking for. Thanks. |
With this test case:
❎
MyAsyncThingTest
fails with this exception:✅
MyAsyncThingFluentTest
passes.Why the difference?
Assert.Throws
returns the exception for further testing, which is useful functionality that theAssert.That
style does not provide.The text was updated successfully, but these errors were encountered: