Skip to content

Instantly share code, notes, and snippets.

@modulexcite
Forked from haacked/ThrowsAsync.cs
Created November 9, 2015 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save modulexcite/d6b1b8bcb57727a4024c to your computer and use it in GitHub Desktop.
Save modulexcite/d6b1b8bcb57727a4024c to your computer and use it in GitHub Desktop.
An async version of xUnit's Async.Throws. Use it like so: await ThrowsAsync<AuthenticationException>(async () => await obj.GetStuffAsync());
public async static Task<T> ThrowsAsync<T>(Func<Task> testCode) where T : Exception
{
try
{
await testCode();
Assert.Throws<T>(() => { }); // Use xUnit's default behavior.
}
catch (T exception)
{
return exception;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment