I have an ASP.NET Web API application, with an ApiController that features asynchronous methods, returning Task<> objects and marked with the async keyword.
public class MyApiController : ApiController
{
public async Task<MyData> GetDataById(string id)
{
...
}
}
How can I write NUnit tests for the ApiController’s asynchronous methods? If I need to use another testing framework I’m open for that too. I’m fairly new to .NET unit testing in general, so I’m interested in learning best practices.
It seems to me there is no support built into NUnit 2.6 for testing async methods returning Tasks. The best option I can see right now is to use Visual Studio’s own UnitTesting framework or xUnit.net as both support asynchronous tests.
With the Visual Studio UnitTesting framework I can write asynchronous tests like this: