0

I have an export class that takes HttpContext as a parameter in the constructor.

public ExportClass(IExportProp IE, HttpContext x)
{
        this._Iexport = IE;
        this._pg = x;
}

I am trying to create an Instance of this class in my Unit Test, but I am not able to use HttpContext in Unit Test.

[TestClass]
public class ExportClass_Test
{
    ExportClass exportClass;
    private IExportProp _iep;


    [ClassInitialize]
    public void TestSetup()
    {

       var ctx = System.Web.HttpContext.Current;//Error: The type or namespace name 'HttpContext' does not exist in the namespace 'System.Web' (are you missingan assembly reference?)
        exportClass = new ExportClass();// I need to pass the HttpContext here, but I get get the above error on my HttpContext
    }
}

I added using System.Web, but this does not solve the issue. Is there a simple way to pass an HttpContext this way, or, what is the proper way to accomplish this?

Reeggiie
  • 782
  • 7
  • 16
  • 36
  • unit test runs as exe so there is no http context by default as if in web enthronement. You need to spin out host to get the http context. – T.S. Feb 23 '17 at 15:31
  • 1
    You need to use the abstraction `HttpContextBase` as the parameter. You can then create a mock instance for testing. – NightOwl888 Feb 23 '17 at 15:33
  • What do you need to access in the HttpContext? abstract that out into a service that can be mocked/faked/stubbed for isolated unit tests. – Nkosi Feb 23 '17 at 16:02

0 Answers0