I've spent already half day on this without any success. How do you unit test routes in Web API?
I mean given the following URI:
~/Test/Get/2
I want to unit test that the above url is captured by the Test controller and Get action accepting an int parameter.
For testing the routes, I have created a library to help you with the assertions - MyWebApi: https://github.com/ivaylokenov/MyWebApi
Basically, you can do the following:
MyWebApi
.Routes()
.ShouldMap(“api/WebApiController/SomeAction/5”)
.WithJsonContent(@”{“”SomeInt””: 1, “”SomeString””: “”Test””}”)
.And()
.WithHttpMethod(HttpMethod.Post)
.To(c => c.SomeAction(5, new RequestModel
{
SomeInt = 1,
SomeString = “Test”
}));
If you want to implement it yourself, you can see the code here and copy it: https://github.com/ivaylokenov/MyWebApi/blob/master/src/MyWebApi/Utilities/RouteResolvers/InternalRouteResolver.cs
I described a ready-to-use example of how to test Web API routes in the related question: Testing route configuration in ASP.NET WebApi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With