小编典典

在测试Init方法中模拟HttpContext.Current

c#

我试图将单元测试添加到已构建的ASP.NET MVC应用程序中。在单元测试中,我使用以下代码:

[TestMethod]
public void IndexAction_Should_Return_View() {
    var controller = new MembershipController();
    controller.SetFakeControllerContext("TestUser");

    ...
}

使用以下助手来模拟控制器上下文:

public static class FakeControllerContext {
    public static HttpContextBase FakeHttpContext(string username) {
        var context = new Mock<HttpContextBase>();

        context.SetupGet(ctx => ctx.Request.IsAuthenticated).Returns(!string.IsNullOrEmpty(username));

        if (!string.IsNullOrEmpty(username))
            context.SetupGet(ctx => ctx.User.Identity).Returns(FakeIdentity.CreateIdentity(username));

        return context.Object;
    }

    public static void SetFakeControllerContext(this Controller controller, string username = null) {
        var httpContext = FakeHttpContext(username);
        var context = new ControllerContext(new RequestContext(httpContext, new RouteData()), controller);
        controller.ControllerContext = context;
    }
}

该测试类继承自具有以下内容的基类:

[TestInitialize]
public void Init() {
    ...
}

在此方法内部,它调用一个库(我无法控制),该库尝试运行以下代码:

HttpContext.Current.User.Identity.IsAuthenticated

现在您可能可以看到问题了。我已经针对控制器设置了伪造的HttpContext,但是没有在此基本的Init方法中设置。单元测试/模拟对我来说还很陌生,所以我想确保自己做对了。对我来说,模拟HttpContext以便在控制器和在Init方法中调用的所有库之间共享它的正确方法是什么?


阅读 292

收藏
2020-05-19

共1个答案

小编典典

HttpContext.Current返回的实例System.Web.HttpContext,该实例不扩展System.Web.HttpContextBaseHttpContextBase后来添加,以解决HttpContext难以嘲笑的问题。这两个类基本上是不相关的(HttpContextWrapper用作它们之间的适配器)。

幸运的是,HttpContext它本身就是可伪造的,足以替换IPrincipal(User)和IIdentity

即使在控制台应用程序中,以下代码也会按预期运行:

HttpContext.Current = new HttpContext(
    new HttpRequest("", "http://tempuri.org", ""),
    new HttpResponse(new StringWriter())
    );

// User is logged in
HttpContext.Current.User = new GenericPrincipal(
    new GenericIdentity("username"),
    new string[0]
    );

// User is logged out
HttpContext.Current.User = new GenericPrincipal(
    new GenericIdentity(String.Empty),
    new string[0]
    );
2020-05-19