AnthonySteele / MvcRouteTester

A library for unit testing ASP MVC route tables for both Web and API Routes
Apache License 2.0
105 stars 43 forks source link

Fails on WEBApi routes that work in application #50

Open btrepp opened 10 years ago

btrepp commented 10 years ago

I have a Web project and a Test Project. I'm using webapi in the web project and MVCRoute tester in the second.

Test is

[TestFixture]
 class TestApiRoute
    {
        HttpConfiguration config;
        [SetUp]
        public void Setup()
        {
            RouteAssert.UseAssertEngine(new NunitAssertEngine());
            config = new HttpConfiguration();
            WebApiConfig.Register(config);
            config.EnsureInitialized();
        }

        [Test]
        public void HasResourceApiRoute()
        {
            RouteAssert.HasApiRoute(config, "/api/resource", HttpMethod.Get);
        }
}

WebAPIConfig

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

This test fails with Route with controller not found for url 'http://site.com/api/resource' but if I run the project the route/controller work fine.

Obviously I done the setup incorrect, but I'm not sure where, and the documentation isn't very clear on how you should setup 'config'

Marusyk commented 9 years ago

I have the same problem. Could you please suggest me how can I solve it?

btrepp commented 9 years ago

@Marusyk I have moved away from testing routes. Currently I use the OWIN framework, so I'm using the OWIN test server and mocking out service layer things (by using a dummy startup.cs). This is how I verify I have routes and that serialization is working correctly in WebApi.

It's super heavy, but it's the only reliable solution I have found to verify that that part of webapi/mvc actually works correctly via tests!

AnthonySteele commented 9 years ago

Some time I want to look at this, and re-do it in vNext. I hope that ASP MVC vNext will offer a way to make a http request testing library that isn't either heavyweight and black-box, or fragile and hacky like this lib. But I don't know enough about vNext yet.

ivaylokenov commented 8 years ago

I think you are trying to reinvent the wheel with model binding with your own algorithm. You can let the Web API framework do the heavy lifting for you. I have implemented a route testing in one of my projects. You can take a look, more specifically in the GetActionContext method where you make the Web API do the model binding for you, depending on the request headers: https://github.com/ivaylokenov/MyWebApi/blob/master/src/MyWebApi/Utilities/RouteResolvers/InternalRouteResolver.cs

AnthonySteele commented 8 years ago

It's true that this code is reaching its limits. But so are the internals of ASP.Net that it works with- vNext will change all of this.