如何使用Moq模拟ASP.NET MVC中的HttpContext?

[TestMethod] public void Home_Message_Display_Unknown_User_when_coockie_does_not_exist() { var context = new Mock<HttpContextBase>(); var request = new Mock<HttpRequestBase>(); context .Setup(c => c.Request) .Returns(request.Object); HomeController controller = new HomeController(); controller.HttpContext = context; //Here I am getting an error (read only). ... } 

我的基础控制器有一个重载的Initialize,得到这个requestContext。 我正试图通过这一点,但我没有做正确的事情。

 protected override void Initialize(System.Web.Routing.RequestContext requestContext) { base.Initialize(requestContext); } 

我在哪里可以得到更多关于使用Moq嘲弄我的RequestContext和HttpContext的信息? 我试图嘲笑cookies和一般情况。

HttpContext是只读的,但它实际上是从您可以设置的ControllerContext派生的。

  controller.ControllerContext = new ControllerContext( context.Object, new RouteData(), controller ); 

创build一个请求,响应并把它们都放到HttpContext中:

 HttpRequest httpRequest = new HttpRequest("", "http://mySomething/", ""); StringWriter stringWriter = new StringWriter(); HttpResponse httpResponse = new HttpResponse(stringWriter); HttpContext httpContextMock = new HttpContext(httpRequest, httpResponse); 

感谢用户0100110010101。

它为我工作,在这里我有一个问题,而编写以下代码的testing用例:

  var currentUrl = Request.Url.AbsoluteUri; 

这是解决问题的线路

 HomeController controller = new HomeController(); //Mock Request.Url.AbsoluteUri HttpRequest httpRequest = new HttpRequest("", "http://mySomething", ""); StringWriter stringWriter = new StringWriter(); HttpResponse httpResponse = new HttpResponse(stringWriter); HttpContext httpContextMock = new HttpContext(httpRequest, httpResponse); controller.ControllerContext = new ControllerContext(new HttpContextWrapper(httpContextMock), new RouteData(), controller); 

可能对别人有帮助。

下面是一个如何设置的例子: 模拟HttpContext HttpRequest和HttpResponse for UnitTests(使用Moq)

注意扩展方法真的有助于简化这个嘲笑类的使用:

 var mockHttpContext = new API_Moq_HttpContext(); var httpContext = mockHttpContext.httpContext(); httpContext.request_Write("<html><body>".line()); httpContext.request_Write(" this is a web page".line()); httpContext.request_Write("</body></html>"); return httpContext.request_Read(); 

下面是如何使用moq编写unit testing的例子,以检查HttpModule是否按预期工作: unit testing使用Moq来包装HttpRequest的HttpModule

更新:这个API已被重构为

  • HttpContextFactory.cs
  • 这些多个Http * 扩展方法文件

以下是我如何使用ControllerContext传递一个虚假的应用程序path:

 [TestClass] public class ClassTest { private Mock<ControllerContext> mockControllerContext; private HomeController sut; [TestInitialize] public void TestInitialize() { mockControllerContext = new Mock<ControllerContext>(); sut = new HomeController(); } [TestCleanup] public void TestCleanup() { sut.Dispose(); mockControllerContext = null; } [TestMethod] public void Index_Should_Return_Default_View() { // Expectations mockControllerContext.SetupGet(x => x.HttpContext.Request.ApplicationPath) .Returns("/foo.com"); sut.ControllerContext = mockControllerContext.Object; // Act var failure = sut.Index(); // Assert Assert.IsInstanceOfType(failure, typeof(ViewResult), "Index() did not return expected ViewResult."); } }