NOTEBOOK HEAVY

ASP.NET Web API In-Memory Hosting Integration Test and Ninject

A while back, I posted how I used StructureMap along with the new ASP.NET self-host features to do integration tests on my ASP.NET Web API. Since that post, I’ve tried out Ninject with some new projects and, thanks to Filip, I discovered In Memory Web Hosting. This post will go over how to integration test an ASP.NET Web API project that uses Ninject for IoC and in memory hosting. I borrow from Filip’s post with adaptations for my purposes. Specifically, I’m using Ninject for IoC and Nunit.

What’s In Memory?

In-memory web hosting is similar to the self-host except it’s all done — wait for it — in memory. Shocking! With self-host, you had to set up an HTTP endpoint on localhost and everything communicated over that. One way to tell this worked was that, in Fiddler, traffic to your self-host would show up as typical HTTP traffic.

In memory hosting differs and doesn’t require that HTTP endpoint. It’s simpler to set up, faster, and, to prove that it’s not doing the same thing as self-host or communicating with a localhost port, no traffic shows up in Fiddler. I’m not going to show that, so you need to trust me.


My ASP.NET Web API is typical. It uses Ninject for IoC and that’s all set up in the App_Start folder. I’m not going to go into much detail about that here, but for more information on that, see another post by Filip and check out the source code for this post on GitHub. In my example code, the Controller and IoC is completely contrived. It’s merely there to show that Ninject is doing its job. Your project would have real work to do, but the code would look similar. Here’s what the controller looks like:

Over to the Integration Test. Make sure to add Nunit from NuGet for the test framework. Also, I use the beta Nunit Visual Studio 2012 integration extension for a test runner to make sure I get that sweet, sweet code coverage analysis. It’s not necessary for this post. Time to set up the in-memory host. You ready? Let’s do this!

Set Up The Server

Here’s the base class that sets up the in-memory host:

You’ll see that it’s very similar to self-host. There’s a HttpConfiguration that includes the route configuration. This time, since Ninject is the IoC framework, there’s a different set of steps than what I used for StructureMap, though it’s essentially the same. Since I already have my IoC bindings setup in the ASP.NET Web API project, I’m just going to reference that code since it’s the same code that is used in production. To make the bootstrap accessible from my test project, I made it public, rather than its original private. Sometimes you just gotta do what you gotta do.

This allows me to pull up a reference to it and access the Kernel. Once I have a reference to the Kernel, I can set it as the DependencyResolver for my in-memory host.

Test Me

Here’s what the actual test looks like:

From this point, I have two different methods for working with the in-memory host. One example, for a GET request, uses a simple set of headers in the HttpRequestMessage (the CreateRequest is taken from Filip).

The other, a POST, serializes a Dto object into JSON (using JSON.NET…don’t forget that reference) before sending it as string content. Both do the same thing, just in different ways. Then I’ve got a bunch of Asserts to make sure the Web API is sending back the right stuff. One thing to note on this POST. Notice that in the SandwichController, the POST action is expecting a concrete type, not an Interface. At one point I had an ISandwich as the parameter, but the automatic binding kept coming up null.

All greens! Time to go get a sandwich. As usual, I’ve posted all of the source code for this in GitHub.

Image Credit: 32Mbit EPROM ST Microelectronics M27C322 by yellowcloud (CC BY 2.0)

Comments

  1. Jeff S.

    In the BaseControllerTest SetupHttpServer() method, you call NinjectWebCommon.Start(), which calls the Bootstrapper.Initialize() method internally. However, you never call NinjectWebCommon.Stop(), which calls Boostrapper.ShutDown(). Does n’t that seem problematic (the ShutDown() method disposes of the IKernel, releases other resources).

    Reply

Reply

Your email address will not be published.