2

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;
    }

}
4
  • That blog is using a 3rd party library to test the routes. You could basically do an in-memory integration test using HttpServer. Does your controller have any dependencies? Show the controller under test, how your tried to test it in a minimal reproducible example and let's see if we can help. Commented Jul 24, 2017 at 13:17
  • Added code above what i tried. Commented Jul 24, 2017 at 14:16
  • That is the test? Commented Jul 24, 2017 at 14:26
  • Inside test method, calling this private method to retrive action info and then, will validate in testmethod for route and httpmethod types. Commented Jul 24, 2017 at 14:26

1 Answer 1

1

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

Thanks.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.