110
[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).
    ...
 }

我的基本控制器覆盖了获取此 requestContext 的 Initialize。我试图传递这个,但我没有做正确的事情。

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

我在哪里可以获得有关使用 Moq 模拟我的 RequestContext 和 HttpContext 的更多信息?我正在尝试模拟 cookie 和一般上下文。

4

6 回答 6

64

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

 controller.ControllerContext = new ControllerContext( context.Object, new RouteData(), controller );
于 2009-09-21T01:05:07.267 回答
43

创建请求、响应并将它们都放入 HttpContext:

HttpRequest httpRequest = new HttpRequest("", "http://mySomething/", "");
StringWriter stringWriter = new StringWriter();
HttpResponse httpResponse = new HttpResponse(stringWriter);
HttpContext httpContextMock = new HttpContext(httpRequest, httpResponse);
于 2009-09-21T14:30:50.390 回答
16

感谢用户 0100110010101。

它对我有用,在这里我在为以下代码编写测试用例时遇到了问题:

 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);

可能对其他人有帮助。

于 2015-06-30T12:02:55.237 回答
7

以下是我使用 ControllerContext 传递虚假应用程序路径的方法:

[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.");
    }
}
于 2011-10-27T21:51:42.070 回答
5

以下是如何设置的示例:Mocking HttpContext HttpRequest and HttpResponse for UnitTests (using 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 编写单元测试以检查 HttpModule 是否按预期工作的示例:Unit Test for HttpModule using Moq to wrap HttpRequest

更新:此 API 已重构为

于 2011-04-10T03:21:17.510 回答
0
var httpResponse = new HttpResponseMessage(HttpStatusCode.OK);
httpResponse.Content = new StringContent("Your response text");
于 2021-08-13T18:10:59.950 回答