Tuesday, April 24, 2012

Unit testing Async code

.NET 4.5 introduces the async and await keywords allowing you to write asynchronous code in a synchronous matter.
But how do I test this asynchronous code? Do I have to add Thread.Sleep() and other kind of erroneous structures everywhere to be able to validate this code?
Visual Studio 11 makes this easy by supporting async testing out-of-the-box.  You can now include “async” keyword part of the test method, and the “await” keyword within the test method body.
A sample: 
public async Task TestAsyncFunctionality()
 var result= await GoOutAndCallMySlowWebservice();

That’s it!

No comments: