how to stub HttpControllerContext

我的梦境 提交于 2019-11-30 05:18:54
Karhgath

You can simply instantiate an HttpControllerContext and assign context objects to it, in addition to route information (you could mock all of these):

var controller = new TestController();
var config = new HttpConfiguration();
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/test");
var route = config.Routes.MapHttpRoute("default", "api/{controller}/{id}");
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "test" } });

controller.ControllerContext = new HttpControllerContext(config, routeData, request);
controller.Request = request;
controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

// Call your method to test
MethodToTest(controller);

HttpControllerContext is simply a container so it does not have to be mocked itself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!