Unit Testing Web Services - HttpContext

后端 未结 5 884
日久生厌
日久生厌 2021-01-31 00:20

I want to write unit tests for a web service. I create my test project, reference my web project (not service reference, assembly reference), then write some code to test the we

5条回答
  •  萌比男神i
    2021-01-31 01:02

    Based on the solution above, I've implemented a wrapper class in the O2 Platform that allows the easy use of these mocking classes, for example this is how I can write and read from the HttpRequest.InputStream

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

    see this blog post for more details: http://o2platform.wordpress.com/2011/04/05/mocking-httpcontext-httprequest-and-httpresponse-for-unittests-using-moq/

提交回复
热议问题