title: Using NUnit to perform integration tests with ASP.NET WebApi controllers
body: <p>I'm trying to setup a test using NUnit to perform some integration testing of ASP.NET WebApi controllers.   I've found a couple articles discussing In-Memory hosting using HttpServer that appears to simplify things by not needing a web server hosting everything.</p>
  
  <ul>
  <li><a href="http://pfelix.wordpress.com/2012/03/05/asp-net-web-api-in-memory-hosting/" rel="noreferrer">ASP.NET Web API: in-memory hosting</a></li>
  <li><a href="http://www.strathweb.com/2012/06/asp-net-web-api-integration-testing-with-in-memory-hosting/" rel="noreferrer">ASP.NET Web API integration testing with in-memory hosting</a></li>
  </ul>
  
  <p><strong>The problem is the only response I ever get is 404-Not Found.</strong>  </p>
  
  <p>The controller are working when manually tested via a browser or Fiddler.  The route definition was copied from the working site.  The api project is referenced by the test project and the dll is getting copied to the same folder as the tests.</p>
  
  <p>Thanks in advance.</p>
  
  <p>Here's the test class</p>
  
  <pre><code>[TestFixture]
  public class InMemoryTests
  {
      private HttpServer Server;
      private string UrlBase = "http://some.server/";
  
      [TestFixtureSetUp]
      public void Setup()
      {
  
          var config = new HttpConfiguration();
          config.Routes.MapHttpRoute(name: "Default", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional });
          config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
  
          Server = new HttpServer(config);
      }
  
      [Test]
      public void GetOrderStatus()
      {
          var client = new HttpClient(Server);
          var request = createRequest("api/Orders/GetOrderStatus?companyCode=001&amp;orderNumber=1234", "application/json", HttpMethod.Get);
  
          using (HttpResponseMessage response = client.SendAsync(request).Result)
          {
              Assert.IsNotNull(response);
              Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
              Assert.NotNull(response.Content);
          }
      }
  
      private HttpRequestMessage createRequest(string url, string mthv, HttpMethod method)
      {
          var request = new HttpRequestMessage();
  
          request.RequestUri = new Uri(UrlBase + url);
          request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(mthv));
          request.Method = method;
  
          return request;
      }
  
      private HttpRequestMessage createRequest&lt;T&gt;(string url, string mthv, HttpMethod method, T content, MediaTypeFormatter formatter) where T : class
      {
          HttpRequestMessage request = createRequest(url, mthv, method);
          request.Content = new ObjectContent&lt;T&gt;(content, formatter);
  
          return request;
      }
  
      public void Dispose()
      {
          if (Server != null)
          {
              Server.Dispose();
          }
      }
  }
  </code></pre>