0

I have a class that implements IHttpHandler interface in .NET 4.7.1. The class implements ProcessRequest which accepts an HttpContext instance.

I am trying to create a test for ProcessRequest using Moq. Moq allows me to create a mock for httpContextBase but not HttpContext.

Mock<HttpContext> mockHttpContext = new Mock<HttpContext>();

Run time error:

Type to mock (HttpContext) must be an interface, a delegate, or a non-sealed, non-static class.

If I mock HttpContextBase, eventually calling the method and passing it will give an error:

Cannot convert from 'System.Web.HttpContextBase' to 'System.Web.HttpContext'

Is there a way to mock HttpContext? I can't modify the class or the method as it's not my code.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Tony_Henrich
  • 42,411
  • 75
  • 239
  • 374
  • Could you please share us with the to-be-tested code as well? – Peter Csala Aug 01 '23 at 10:52
  • 1
    Can't you simply create an `HttpContext` via [that constructor](https://learn.microsoft.com/en-us/dotnet/api/system.web.httpcontext.-ctor?view=netframework-4.7.1#system-web-httpcontext-ctor(system-web-httprequest-system-web-httpresponse)) which anticipates an `HttpRequest` and an `HttpResponse` objects? – Peter Csala Aug 01 '23 at 10:57

0 Answers0