Q:

Unit test Custom Route in Web API

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.

A:

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

