10

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.

The problem is the only response I ever get is 404-Not Found.

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.

Thanks in advance.

Here's the test class

[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&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<T>(string url, string mthv, HttpMethod method, T content, MediaTypeFormatter formatter) where T : class
    {
        HttpRequestMessage request = createRequest(url, mthv, method);
        request.Content = new ObjectContent<T>(content, formatter);

        return request;
    }

    public void Dispose()
    {
        if (Server != null)
        {
            Server.Dispose();
        }
    }
}
2
  • this looks OK. Difficult to say without seeing the controller. Could you post definition for this action /api/Orders/GetOrderStatus?companyCode=001&orderNumber=1234 ? Commented Jul 26, 2012 at 21:57
  • Nothing really special about the controller. It all works when testing manually. Also I get the same result no matter which controller I test. Commented Jul 27, 2012 at 0:28

1 Answer 1

9

I was seeing this problem also, seemed to go away when I moved my test class into the same assembly as my controller; generally not practical for testing I know.

After a bit of digging it appears there's a bug that only occurs with self host when the calling code doesn't share the same assembly as the controller as it hasn't managed to load the required assemblies.

To confirm this is your problem / temporarily workaround add this as the first line of your test: -

Type myType = typeof(myControllerType);

More info at : http://forums.asp.net/t/1772734.aspx/1

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

2 Comments

Marking your answer as correct. I had posted my question on the asp.net forums too and got the same answer. I had to do a little more work because I also had to ensure all the dependencies required by your controllers are available (duh!) forums.asp.net/t/1829098.aspx/…
Why is this the case? I also had this where if I put the test method in the same assembly it worked, but if I put it elsewhere, as in the test project, it would not work.

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.