0

I have a WebApi project that I am trying to test. I have a controller -

public class UserController : ApiController
{
    public string Get()
    {
        return "foo";
    }
}

I need to test the route and I am trying to use WebApiContrib.Testing package.

Now, my problem is that while registering routes, I use GlobalConfiguration.Configuration.Routes which is of type HttpRouteCollection.

In my test, I can access RouteTable.Routes but this is of type RouteCollection.

As a result, I can't register the WebApi routes in my test, because of this type incompatibility.

Has anyone encountered this issue before? Is there something obvious that I am missing?

Srikanth Venugopalan
  • 9,011
  • 3
  • 36
  • 76

1 Answers1

3

I resolved this by using:

WebApiConfig.Register( GlobalConfiguration.Configuration );

In TestFixtureSetup

Maate
  • 1,000
  • 1
  • 6
  • 15
  • This works fine until you have two test fixtures that need to register the configuration ;) Do you know how to "unregister" or "dispose" of a previous registration? – robbymurphy Jul 03 '13 at 18:40
  • 1
    You just need to call `GlobalConfiguration.Configuration.Routes.Clear()` before you call `WebApiConfig.Register( GlobalConfiguration.Configuration )` to avoid the routes getting mapped multiple times. – robbymurphy Jul 05 '13 at 13:18
  • This makes the Get based tests work, but I'm not having any joy with POST etc - can anyone offer any insight? Details here - http://stackoverflow.com/questions/18377365/testing-asp-net-web-api-post-and-parameterless-get-routes-with-webapicontrib-tes – Keith Jackson Aug 30 '13 at 07:41