public async Task SomethingAsync_Throws_Exception() { var sut = new AsynchronousError(); await Assert.ThrowsAsync <InvalidOperationException>(async() => { await sut.SomethingAsync(); }); }
public void SomethingAsync_Return_Exception_AsyncContext() { var sut = new AsynchronousError(); // using Nito.AsyncEx; is a help libary for async // it can help with testing async methods to avoid deadlocks a race conditions AsyncContext.Run(async() => { await Assert.ThrowsAsync <InvalidOperationException>(async() => { // Do not forget to await the task returned by ThrowAsync! // If you forget to you test may pass silently a false positive await sut.SomethingAsync(); }); }); }