Category Archives: ASP.Net

How to mock HttpContext.User

var fakeHttpContext = new Mock<HttpContextBase>(); var fakeIdentity = new GenericIdentity(“User”); var principal = new GenericPrincipal(fakeIdentity, null); fakeHttpContext.Setup(t => t.User).Returns(principal); var controllerContext = new Mock<ControllerContext>(); controllerContext.Setup(t => t.HttpContext).Returns(fakeHttpContext.Object); _requestController = new RequestController(); //Set your controller ControllerContext with fake context _requestController.ControllerContext = … Continue reading

Advertisement

Posted in ASP.Net | Leave a comment