AFAICT,微软只是忘记包含它。它当然应该在 IMO 那里(如果您同意,请对 UserVoice 投票)。
同时,您可以使用以下方法。它来自我的 AsyncEx 库AsyncAssert
中的类。我计划在不久的将来作为 NuGet 库发布,但现在你可以把它放在你的测试类中:AsyncAssert
public static async Task ThrowsAsync<TException>(Func<Task> action, bool allowDerivedTypes = true)
{
try
{
await action();
Assert.Fail("Delegate did not throw expected exception " + typeof(TException).Name + ".");
}
catch (Exception ex)
{
if (allowDerivedTypes && !(ex is TException))
Assert.Fail("Delegate threw exception of type " + ex.GetType().Name + ", but " + typeof(TException).Name + " or a derived type was expected.");
if (!allowDerivedTypes && ex.GetType() != typeof(TException))
Assert.Fail("Delegate threw exception of type " + ex.GetType().Name + ", but " + typeof(TException).Name + " was expected.");
}
}