我试图为我的行为编写单元测试,但我无法找到检查授权的方法。我的第一次尝试基于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");
}
然而,viewResult.ViewName
是&#34; Home&#34;即使我嘲笑IsAuthenticated
为false
。
HomeController
:
[Authorize]
public class HomeController : ApplicationController
{
public ActionResult Index()
{
return View("Home");
}
...
}