Moq: unit testing a method relying on HttpContext

后端 未结 6 1022
情话喂你
情话喂你 2020-11-29 03:53

Consider a method in a .NET assembly:

public static string GetSecurityContextUserName()
{             
 //extract the username from request              
 st         


        
6条回答
  •  忘掉有多难
    2020-11-29 04:27

    Have a look at this http://haacked.com/archive/2007/06/19/unit-tests-web-code-without-a-web-server-using-httpsimulator.aspx

    Using httpSimulator class,You will be able to do pass a HttpContext to handler

    HttpSimulator sim = new HttpSimulator("/", @"C:\intepub\?")
    .SimulateRequest(new Uri("http://localhost:54331/FileHandler.ashx?
    ticket=" + myticket + "&fileName=" + path));
    
    FileHandler fh = new FileHandler();
    fh.ProcessRequest(HttpContext.Current);
    

    HttpSimulator implement what we need to get a HttpContext instance. So you don't need to use Moq here.

提交回复
热议问题