Carolyn Van Slyck

Unit testing your ASP.NET Web API routes

When your API is purely RESTful, your routes are pretty simple and there isn't much need to test. But if you are like me and decided to mix RPC and RESTful style methods on the same controller, then some tests are in order.

I had been using the RouteTestingExtensions from MvcContrib to test my MVC routes. It allows you to assert that a URL was routed to the expected controller and action. I modified it to work with Web API routes and contributed it to the sister project, WebApiContrib. The package, WebApiContrib.Testing is available via NuGet.

Below is an example of how to use this extension method to validate your url: