我如何单元测试,使用疗法Controller.User变量控制器的动作? [英] How do I unit test a controller action that uses ther Controller.User variable?

查看:123
本文介绍了我如何单元测试,使用疗法Controller.User变量控制器的动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器操作,可自动重定向到一个新的页面,如果已登录用户的( User.Identity.IsAuthenticated )。什么是写单元测试这种情况下,确保了重定向需要的地方的最佳方式是什么?

I have a controller action that automatically redirects to a new page if the user is already logged in (User.Identity.IsAuthenticated). What is the best way to write a unit test for this scenario to ensure that the redirect takes places?

推荐答案

我一直在使用与起订量以下嘲弄已经允许设立各种条件在我的单元测试。首先,HttpContextBase模拟:

I've been using the following Mocks with Moq to allow setting up various conditions in my unit tests. First, the HttpContextBase mock:

    public static Mock<HttpContextBase> GetHttpContextMock(bool isLoggedIn)
    {
        var context = new Mock<HttpContextBase>();
        var request = new Mock<HttpRequestBase>();
        var response = new Mock<HttpResponseBase>();
        var session = new Mock<HttpSessionStateBase>();
        var server = new Mock<HttpServerUtilityBase>();
        var principal = AuthenticationAndAuthorization.GetPrincipleMock(isLoggedIn);

        context.SetupGet(c => c.Request).Returns(request.Object);
        context.SetupGet(c => c.Response).Returns(response.Object);
        context.SetupGet(c => c.Session).Returns(session.Object);
        context.SetupGet(c => c.Server).Returns(server.Object);
        context.SetupGet(c => c.User).Returns(principal.Object);

        return context;
    }

这可能提供了一个有用的模拟每一个属性设置在这里。这样一来,如果我需要添加类似引荐,我就可以使用:

Every property that might provide a useful Mock is set up in here. That way, if I need to add something like a referrer, I can just use:

Mock.Get(controller.Request).Setup(s => s.UrlReferrer).Returns(new Uri("http://blah.com/");

在GetPrincipleMock​​的方法是建立用户。它看起来是这样的:

The "GetPrincipleMock" method is what sets up the user. It looks like this:

    public static Mock<IPrincipal> GetPrincipleMock(bool isLoggedIn)
    {
        var mock = new Mock<IPrincipal>();

        mock.SetupGet(i => i.Identity).Returns(GetIdentityMock(isLoggedIn).Object);
        mock.Setup(i => i.IsInRole(It.IsAny<string>())).Returns(false);

        return mock;
    }

    public static Mock<IIdentity> GetIdentityMock(bool isLoggedIn)
    {
        var mock = new Mock<IIdentity>();

        mock.SetupGet(i => i.AuthenticationType).Returns(isLoggedIn ? "Mock Identity" : null);
        mock.SetupGet(i => i.IsAuthenticated).Returns(isLoggedIn);
        mock.SetupGet(i => i.Name).Returns(isLoggedIn ? "testuser" : null);

        return mock;
    }

现在,在测试中我的控制器的设置是这样的:

Now, my controller setups in the tests look like this:

var controller = new ProductController();
var httpContext = GetHttpContextMock(true); //logged in, set to false to not be logged in

ControllerContext controllerContext = new ControllerContext(httpContext.Object, new RouteData(), controller);
controller.ControllerContext = controllerContext;

这是冗长设置了一点点,但一旦你拥有了一切就绪,测试各种条件变得轻松了许多。

It's a little bit of verbose setup, but once you have everything in place, testing a variety of conditions becomes a lot easier.

这篇关于我如何单元测试,使用疗法Controller.User变量控制器的动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆