I suggest you ...

Add an async-compatible Assert.ThrowsException

Win8 replaced ExpectedExceptionAttribute with Assert.ThrowsException. This is good, but there isn't an equivalent Assert.ThrowsExceptionAsync for async delegates.

Something like this would be fine:

public static async Task ThrowsExceptionAsync<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.");
}
}

31 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Stephen ClearyStephen Cleary shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    2 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...

      Feedback and Knowledge Base