// Reference: http://nerddinnerbook.s3.amazonaws.com/Part12.htm // Yes .. Nerd Dinner to the rescue! and we come full circle... public static void SetUpControllerContext(ControllerBase controller, string userId = null, string displayName = null, string[] roles = null) { Condition.Requires(controller); // Some fake Authentication stuff. var customIdentity = new CustomIdentity(userId, displayName); var customPrincipal = new CustomPrincipal(customIdentity, roles); var mockControllerContext = new Mock<ControllerContext>(); mockControllerContext.Setup(x => x.HttpContext.User).Returns(customPrincipal); controller.ControllerContext = mockControllerContext.Object; }
// Reference: http://nerddinnerbook.s3.amazonaws.com/Part12.htm // Yes .. Nerd Dinner to the rescue! and we come full circle... private static HomeController HomeController(IDocumentSession documentSession, int userId = 0, string displayName = null, string[] roles = null) { Condition.Requires(documentSession); // Some fake Authentication stuff. var customIdentity = new CustomIdentity(userId, displayName); var customPrincipal = new CustomPrincipal(customIdentity, roles); var mockControllerContext = new Mock<ControllerContext>(); mockControllerContext.Setup(x => x.HttpContext.User).Returns(customPrincipal); var homeController = new HomeController(documentSession) {ControllerContext = mockControllerContext.Object}; return homeController; }