I am attempting to create a unit test for my controller, but the action I am testing uses a partial view to string function which doesn\'t want to work in my tests.
Final solution thanks to help in the comments.
var mock = new Mock();
mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns(userName);
if (userName != null)
{
mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);
mock.SetupGet(p => p.HttpContext.User.Identity.IsAuthenticated).Returns(true);
}
else
{
mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(false);
}
var routeData = new RouteData();
routeData.Values.Add("controller", "BlogController");
mock.SetupGet(m => m.RouteData).Returns(routeData);
var view = new Mock();
var engine = new Mock();
var viewEngineResult = new ViewEngineResult(view.Object, engine.Object);
engine.Setup(e => e.FindPartialView(It.IsAny(), It.IsAny(), It.IsAny())).Returns(viewEngineResult);
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(engine.Object);
var controller = new BlogController();
controller.ControllerContext = mock.Object;