xUnit's Assert.Throws
(at least on version 1.9.2) is not async-aware. This was fixed in version 2, which now has an Assert.ThrowsAsync
method.
So, you can either upgrade to xUnit 2 or create your own method to get it working:
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;
}
await ThrowsAsync<InvalidOperationException>(async () => await UserRepository.CreateAsync(testUser));
From Haacked's gist.