【发布时间】:2016-08-10 19:07:59
【问题描述】:
我使用的是 NUnit 3。我写了一个扩展方法:
public static T ShouldThrow<T>(this TestDelegate del) where T : Exception {
return Assert.Throws(typeof(T), del) as T;
}
这允许我这样做:
TestDelegate del = () => foo.doSomething(null);
del.ShouldThrow<ArgumentNullException>();
现在我想要类似的异步:
AsyncTestDelegate del = async () => await foo.doSomething(null);
del.ShouldThrowAsync<ArgumentNullException>();
所以我写了这个:
public static async Task<T> ShouldThrowAsync<T>(this AsyncTestDelegate del) where T : Exception {
return (await Assert.ThrowsAsync(typeof(T), del)) as T;
}
但这不起作用:'Exception' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'Exception' could be found (are you missing a using directive or an assembly reference?)。
我做错了什么?
【问题讨论】:
标签: c# unit-testing asynchronous async-await nunit