ASP.Net Web Api Integration Tests With Self Hosting

I am trying to help out a team to verify some of their stuff works with the new ASP.Net Web API 4 Beta.  While they seem to have a number of unit tests of good quality, there was a dearth of integration tests.  I set out to resolve that as simply as possible.

To clarify we are living here:

   image

For full UI/user tests I’d deploy the whole app, but I still want my Functional Integration tests to be faster and run locally with ease if I can.  For Web API, I was hoping for self hosting.  WCF can do this, but WCF is a giant pile of mud that I’m not interested in attacking.  So I went looking for Web API Self Hosting for Dummies.  Found it.

And here’s how you use it:image

Note: I’ve set the folder = “” in this sample since my ApiController is side-by-side with my test fixture.  Assuming your ApiController is in a different assembly than your tests, you can sepcify the namespace: parameter in the MapHttpRoute like this:

namespaces: new[] { typeof(ThingsController).Namespace }

  • http://hammerproject.com/ matt kocaj

    This is good but it seems as though HttpSelfHostServer instantiates the 
    controllers directly and controllers with any dependencies will break this methodology. Is it possible to inject dependencies via IoC with a test like this?

    • cromwellryan

      I’m using this technique with a series of Specflow specs to build acceptance tests around an api.  I don’t use an IoC (yet) and instead have a default constructor with the appropriate dependencies.  I imagine that will change as this evolves and when the time comes I’m going to look into the self hosting construction code either directly (is it OSS?) or with Reflector/JustDecompile

  • neel

    how can i integrate cleartrip api with php based website ?

    • http://www.alexjamesbrown.com/ Alex Brown

      This comment has nothing to do with this blog post!?

  • Pingback: Automated Testing of ASP.NET Web API and MVC applications | Robert Daniel Moore's Blog