diff --git a/docs/testing/moq.md b/docs/testing/moq.md index a87f81c..3055a90 100644 --- a/docs/testing/moq.md +++ b/docs/testing/moq.md @@ -9,6 +9,27 @@ grand_parent: My Docs ### How to MOQ a HTTPContext in MVC/API for .Net Framework --- +- We need to set the Controller Context for the controller under test. From that the HTTPContext is pulled. + ```csharp + public static ControllerContext GetControllerContext(Controller controller) + { + var mockHttpContext = new Mock(); + var mockRequest = new Mock(); + + var routeData = new RouteData(); + routeData.Values["controller"] = "Some controller name"; + routeData.Values["action"] = "some action name"; + // feel free to set anything else required for the context like route data or claims etc. + + var requestContext = new RequestContext(mockHttpContext.Object, routeData); + mockHttpContext.Setup(ctx => ctx.Request).Returns(mockRequest.Object); + mockHttpContext.Setup(ctx => ctx.Request.RequestContext).Returns(requestContext); + return new ControllerContext(requestContext, controller) { HttpContext = mockHttpContext.Object }; + } + +// usage inside a test -``` \ No newline at end of file +var _sut = new MyController(); // inject dependencies if any +_sut.ControllerContext = GetControllerContext( _sut); +```