【发布时间】:2020-09-09 12:29:56
【问题描述】:
我在使用异步委托的 NUnit 测试中遇到了一个奇怪的行为。
MyAsyncTest,我事先声明了委托,失败并显示System.ArgumentException : Async void methods are not supported. Please use 'async Task' instead.
MyAsyncTest2,我在其中粘贴委托,通过。
[Test] //fails
public void MyAsyncTest()
{
TestDelegate testDelegate = async () => await MyTestMethod();
Assert.That(testDelegate, Throws.Exception);
}
[Test] //passes
public void MyAsyncTest2()
{
Assert.That(async () => await MyTestMethod(), Throws.Exception);
}
private async Task MyTestMethod()
{
await Task.Run(() => throw new Exception());
}
谁能解释一下?
【问题讨论】:
标签: c# .net async-await nunit