Unit Test with route data not working on ASP.NET MVC 5 Web API

前端 未结 1 538
北恋
北恋 2020-12-16 01:16

I have upgraded my web api project to the latest version, using MVC 5 The application runs properly but this line of code is not working anymore on my unit tests:

         


        
相关标签:
1条回答
  • 2020-12-16 02:05

    It looks like in MVC 5 the Url property is created in a different way. I have introduced this line in my tests and now the Url property is back to normal

    private static void SetupControllerForTests(ApiController controller)
    {
        var config = new HttpConfiguration();
        var request = new HttpRequestMessage(HttpMethod.Post, "http://api.clientele-itsm.com/api/organization");
        var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
        var routeData = new HttpRouteData(route, new HttpRouteValueDictionary
        {
            {"id", Guid.Empty},
            {"controller", "organization"}
        });
        controller.ControllerContext = new HttpControllerContext(config, routeData, request);
        UrlHelper urlHelper = new UrlHelper(request);
        controller.Request = request;
        controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
        controller.Request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData;
        /// inject a fake helper
        controller.Url = urlHelper;
    }
    
    0 讨论(0)
提交回复
热议问题