Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing for exceptions in async methods

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.

like image 347
user227895 Avatar asked Mar 14 '17 15:03

user227895


People also ask

What happens if an exception is thrown within an asynchronous method?

As we know, in asynchronous programming, control does not wait for the function's result and it executes the next line. So when the function throws an exception, at that moment the program control is out of the try-catch block.

Are exceptions handled asynchronously?

Learn the exception handling semantics for asynchronous methods in C# Exception handling is the technique of handling runtime errors in an application. Asynchronous programming allows us to perform resource-intensive operations without the need for blocking on the main or executing thread of the application.

How do you test async method?

This illustrates the first lesson from the async/await conceptual model: To test an asynchronous method's behavior, you must observe the task it returns. The best way to do this is to await the task returned from the method under test.

Can async method have out parameter?

You can't have async methods with ref or out parameters.


1 Answers

You should use Func<Task> instead of Action:

[Test] public void TestFail() {     Func<Task> f = async () => { await Fail(); };     f.ShouldThrow<Exception>();             } 

That will call the following extension which is used to verify asynchronous methods

public static ExceptionAssertions<TException> ShouldThrow<TException>(     this Func<Task> asyncAction, string because = "", params object[] becauseArgs)         where TException : Exception         

Internally this method will run task returned by Func and wait for it. Something like

try {     Task.Run(asyncAction).Wait(); } catch (Exception exception) {     // get actual exception if it wrapped in AggregateException } 

Note that test itself is synchronous.

like image 53
Sergey Berezovskiy Avatar answered Oct 16 '22 12:10

Sergey Berezovskiy