行动测试忽略授权

时间:2016-12-13 12:50:48

标签: asp.net-mvc unit-testing mocking moq

我试图为我的行为编写单元测试,但我无法找到检查授权的方法。我的第一次尝试基于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;即使我嘲笑IsAuthenticatedfalse

HomeController

[Authorize]
public class HomeController : ApplicationController
{
    public ActionResult Index()
    {
        return View("Home");
    }
   ...
}

0 个答案:

没有答案