Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft.Owin.Testing.TestServer 404 not found

When I make request to TestServer I get 404 response. And I can't understand why, because I use the same config for normal server and it works. For making requests I use TestServer.HttpClient.

like image 576
Andrew Snigur Avatar asked Apr 08 '16 19:04

Andrew Snigur


Video Answer


1 Answers

For me it was that my test didn't know about the thing it was testing. I thought it was good enough to share the project's WebApiConfig with the test, but all that does is give it empty routing information. It doesn't tell the test what it's supposed to test. So it's just going out to http://localhost/api/controller/action and getting a 404 the same as if you tried it in your browser now.

There's almost nothing on how to establish this missing connection. The closest is http://www.juliencorioland.net/archives/using-owin-to-test-your-web-api-controllers , but it glosses over the most important part--how to tell the test what to test!

The example code looks something like this:

class OwinTestConf
{
  public void Configuration(IAppBuilder app)
  {
 HttpConfiguration config = new HttpConfiguration();
    config.Services.Replace(typeof(IAssembliesResolver), new TestWebApiResolver());
    config.MapHttpAttributeRoutes();
    app.UseWebApi(config);
}

}

What's that TestWebApiResolver? Well, you make a class that inherits from DefaultAssembliesResolver and then override GetAssemblies(). In there you load the thing you're trying to test. There's a good post on how to do this here: http://www.strathweb.com/2013/08/customizing-controller-discovery-in-asp-net-web-api/

Copypasting the good stuff from there, your override is going to look like this:

public class MyAssembliesResolver : DefaultAssembliesResolver
{
  public override ICollection<Assembly> GetAssemblies()
 {
    ICollection<Assembly> baseAssemblies = base.GetAssemblies();
    List<Assembly> assemblies = new List<Assembly>(baseAssemblies);
    var controllersAssembly = Assembly.Load("MyAssembly");
    baseAssemblies.Add(controllersAssembly);
    return assemblies;
  }
}

...where 'MyAssembly' is the assembly name of the thing you're trying to test (right click the project -> properties, it's at the top of the first tab)

That gets you over that hurdle.

The second hurdle made testing this way a dealbreaker for me. I finally had a non-404 response, now it was a 500. I knew I had the URLs right because changing them slightly made them 404 again. The problem is there's no way to debug that. Since you're just referencing the assembly you're testing, there's no way to step into it and set breakpoints and what not. What good is a test that can't tell you why it's failing? Especially annoying is that it worked fine in IIS. So I've lost trust that this is a good simulation of how my code will run in the wild.

like image 118
Chad Hedgcock Avatar answered Sep 24 '22 23:09

Chad Hedgcock