fluent-assertions

Testing for exceptions in async methods

不打扰是莪最后的温柔 提交于 2019-11-28 10:39:27
I'm a bit stuck with this code (this is a sample): public async Task Fail() { await Task.Run(() => { throw new Exception(); }); } [Test] public async Task TestFail() { Action a = async () => { await Fail(); }; a.ShouldThrow<Exception>(); } The code doesn't catch the exception, and fails with Expected a System.Exception to be thrown, but no exception was thrown. I'm sure I'm missing something, but docs seem to suggest this is the way to go. Some help would be appreciated. You should use Func<Task> instead of Action : [Test] public void TestFail() { Func<Task> f = async () => { await Fail(); };

Testing for exceptions in async methods

泪湿孤枕 提交于 2019-11-27 03:01:04
问题 I'm a bit stuck with this code (this is a sample): public async Task Fail() { await Task.Run(() => { throw new Exception(); }); } [Test] public async Task TestFail() { Action a = async () => { await Fail(); }; a.ShouldThrow<Exception>(); } The code doesn't catch the exception, and fails with Expected a System.Exception to be thrown, but no exception was thrown. I'm sure I'm missing something, but docs seem to suggest this is the way to go. Some help would be appreciated. 回答1: You should use