-2
    public static string Test()
    {
        var page = HttpContext.Current.Request.QueryString["page"];
        var pageSize = HttpContext.Current.Request.QueryString["pageSize"];
        var sortBy = HttpContext.Current.Request.QueryString["sortBy"];
        var sortOrder = HttpContext.Current.Request.QueryString["sortOrder"];
        var searchBy = HttpContext.Current.Request.QueryString["searchBy"];
        var searchKey = HttpContext.Current.Request.QueryString["searchKey"];
        if (!StandardRegister.AppServices.Security.Authenticator.VerifyAuthentication())
        {
            return null;
        }
  }

unit test

     [Test]
    public async Task GetMaintainKitsTest()
    {
        HttpContext.Current.Request.QueryString.Add("page", "1");


         var maintainKits = Test();


    }

error:Object reference not set to an instance of an object.

so to add HttpContext.Current.Request.QueryString in unit test

  • You will need to mock the HttpContext so you can set values. Just trying to get things out won't work as they won't exist. See here for something similar: https://stackoverflow.com/questions/9624242/setting-httpcontext-current-session-in-a-unit-test – sr28 Feb 19 '20 at 13:24

1 Answers1

0

You need to set a value to HttpContext.Current, otherwise, it will be null. Something like:

HttpContext.Current = new HttpContext(
    new HttpRequest(filename: "", url: "http://some.url", queryString: ""),
    new HttpResponse(new StringWriter())
);
Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74