I'm trying to write unit tests for my actions, but I am unable to find a way to check authorization. My first attempt is based on the accepted answer in this thread:
[Test]
public void Test()
{
var request = new Mock<HttpRequestBase>();
request.SetupGet(x => x.IsAuthenticated).Returns(false);
var context = new Mock<HttpContextBase>();
context.SetupGet(x => x.Request).Returns(request.Object);
var controller = new HomeController();
controller.ControllerContext =
new ControllerContext(context.Object, new RouteData(), controller);
// test
ViewResult viewResult = (ViewResult)controller.Index();
Assert.True(viewResult.ViewName == "Logon");
}
However, viewResult.ViewName is "Home" even though I am mocking IsAuthenticated as false.
HomeController:
[Authorize]
public class HomeController : ApplicationController
{
public ActionResult Index()
{
return View("Home");
}
...
}