【问题标题】:Mock HttpContext.Current in Test Init Method在测试初始化​​方法中模拟 HttpContext.Current
【发布时间】:2011-05-21 17:40:03
【问题描述】:

我正在尝试将单元测试添加到我构建的 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 方法中调用的任何库之间共享它的正确方法是什么。

【问题讨论】:

    标签: c# unit-testing mocking httpcontext


    【解决方案1】:

    HttpContext.Current 返回一个System.Web.HttpContext 的实例,它不扩展System.Web.HttpContextBaseHttpContextBase 后来被添加到地址 HttpContext 难以模拟。这两个类基本上是不相关的(HttpContextWrapper用作它们之间的适配器)。

    幸运的是,HttpContext 本身是可伪造的,足以让您替换 IPrincipal(用户)和 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]
        );
    

    【讨论】:

    • 干杯,但我如何为注销的用户设置这个?
    • @nfplee - 如果将空字符串传递给 GenericIdentity 构造函数,IsAuthenticated 将返回 false
    • 这个可以用来模拟HttpContext中的Cache吗?
    • 嗨@RichardSzalay,我将此sn-p代码添加到我的测试中,然后调用了我的控制器方法,但测试失败,因为'User.Identity.Name'为空。我假设这段代码 sn-p 会导致这个值被填充,我错了还是遗漏了什么?
    • @CiaranG - MVC 使用HttpContextBase,可以模拟。如果您使用 MVC,则无需使用我发布的解决方法。如果你继续它,你可能需要在你创建控制器之前运行我发布的代码。
    【解决方案2】:

    下面的测试初始化​​也将完成这项工作。

    [TestInitialize]
    public void TestInit()
    {
      HttpContext.Current = new HttpContext(new HttpRequest(null, "http://tempuri.org", null), new HttpResponse(null));
      YourControllerToBeTestedController = GetYourToBeTestedController();
    }
    

    【讨论】:

    • 我无法在我的解决方案的单独测试项目中获得对 HTTPContext 的可寻址性。你可以通过继承控制器来获得它吗?
    • 你的测试项目中是否引用了System.Web
    • 是的,但是我的项目是一个 MVC 项目,是否可能 MVC 版本的 System.Web 只包含该命名空间的一个子集?
    • @user1522548(你应该创建一个帐户)Assembly System.Web.dll,v4.0.0.0 肯定有 HTTPContext 我刚刚检查了我的源代码。
    • 我的错误,我在文件中引用了 System.Web.MVC 而不是 System.Web。感谢您的帮助。
    【解决方案3】:

    如果您的应用程序第三方在内部重定向,那么最好通过以下方式模拟 HttpContext:

    HttpWorkerRequest initWorkerRequest = new SimpleWorkerRequest("","","","",new StringWriter(CultureInfo.InvariantCulture));
    System.Web.HttpContext.Current = new HttpContext(initWorkerRequest);
    System.Web.HttpContext.Current.Request.Browser = new HttpBrowserCapabilities();
    System.Web.HttpContext.Current.Request.Browser.Capabilities = new Dictionary<string, string> { { "requiresPostRedirectionHandling", "false" } };
    

    【讨论】:

      【解决方案4】:

      我知道这是一个较老的主题,但是模拟 MVC 应用程序进行单元测试是我们经常做的事情。

      在升级到 Visual Studio 2013 后,我只是想补充一下使用 Moq 4 模拟 MVC 3 应用程序的经验。没有一个单元测试在调试模式下工作,并且 HttpContext 在尝试窥视时显示“无法评估表达式”在变量处。

      原来 Visual Studio 2013 在评估某些对象时存在问题。为了让调试模拟的 Web 应用程序再次运行,我必须检查 Tools=>Options=>Debugging=>General settings 中的“Use Managed Compatibility Mode”。

      我通常会这样做:

      public static class FakeHttpContext
      {
          public static void SetFakeContext(this Controller controller)
          {
      
              var httpContext = MakeFakeContext();
              ControllerContext context =
              new ControllerContext(
              new RequestContext(httpContext,
              new RouteData()), controller);
              controller.ControllerContext = context;
          }
      
      
          private static HttpContextBase MakeFakeContext()
          {
              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 user = new Mock<IPrincipal>();
              var identity = new Mock<IIdentity>();
      
              context.Setup(c=> c.Request).Returns(request.Object);
              context.Setup(c=> c.Response).Returns(response.Object);
              context.Setup(c=> c.Session).Returns(session.Object);
              context.Setup(c=> c.Server).Returns(server.Object);
              context.Setup(c=> c.User).Returns(user.Object);
              user.Setup(c=> c.Identity).Returns(identity.Object);
              identity.Setup(i => i.IsAuthenticated).Returns(true);
              identity.Setup(i => i.Name).Returns("admin");
      
              return context.Object;
          }
      
      
      }
      

      并像这样启动上下文

      FakeHttpContext.SetFakeContext(moController);
      

      然后直接调用控制器中的方法

      long lReportStatusID = -1;
      var result = moController.CancelReport(lReportStatusID);
      

      【讨论】:

      • 是否有充分的理由以这种方式设置它而不是接受的答案?这似乎更复杂,似乎没有提供任何额外的好处。
      • 它提供了更详细/模块化的模拟方法
      • 如果我的被测代码直接引用了HttpContext.Current,并且没有注入基础依赖怎么办?
      猜你喜欢
      • 2019-06-29
      • 2012-10-02
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      相关资源
      最近更新 更多