user3711357
user3711357

Reputation: 1625

How to write unit test for WEB API Http Method and Custom Route

I need to write unit test for, 1) web api method has [HTTPPOST] method type and 2) web api method has route attribute [api/Verification].

Can any one suggest, how to write test to do this.

I found one of the way for web api route verification using http://www.vannevel.net/2015/03/08/50/ but RouteAssert is not found.

I am writing unit test using MSTest.

Tried below way to find descriptor so, can check for route and httpmethods, problem is, its giving null for methodInfo.

private HttpActionDescriptor GetAction(AccountAPIController controller, string name)
{
    try
    {        
        MethodInfo methodInfo = controller.GetType().GetMethod(name, new Type[] { controller.GetType(), controller.GetType() });
        return new ReflectedHttpActionDescriptor { MethodInfo = methodInfo, Configuration = controller.Configuration, ControllerDescriptor = new HttpControllerDescriptor()};
    }
    catch (Exception ex)
    {

        throw;
    }

}

Upvotes: 2

Views: 217

Answers (1)

user3711357
user3711357

Reputation: 1625

I found the solution by reflection to verify for class and method attributes. It works finally.

Thanks.

Upvotes: 1

Related Questions