Mocking HttpListenerContext

I am trying to use unit test code that uses an HttpListenerContext. I cannot find a base class or interface that I can make fun of. Is there something like an HttpRequestBase for an HttpListenerContext?

+4
source share
1 answer

The way to solve the problem is to create an abstract class using virtual methods.

public abstract class HttpListenerContextBase { public virtual HttpListenerRequestBase Request { get; private set; } public virtual HttpListenerResponseBase Response { get; private set; } public virtual IPrincipal User { get; private set; } } 

You can then create your own wrapper class for the real one, which takes an HTTPListnerContext in the constructor and inherits from the HttpListenerContextBase. It simply returns methods from the real context.

In your code, you then write all your methods against the HttpListenerContextBase and mock them.

A little effort, but you get something verifiable.

+2
source

All Articles