且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

单元测试与不工作的ASP.NET MVC的Web 5 API数据路由

更新时间:2023-02-16 22:57:59

它看起来像在MVC 5以不同的方式创建Url属性。
我已经介绍了在我的测试中这行,现在的URL属性恢复正常。

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;
}