Using httpcontext in unit test

后端 未结 4 817
终归单人心
终归单人心 2021-02-13 14:44

I\'m using C#4.0 and i need to unit test a service. The function inside the service returns a path similar to the variable i called expected, this is the path i\'m expecting to

4条回答
  •  迷失自我
    2021-02-13 14:57

    At the moment I can't find my full wrapper for HttpContext that I used earlier, but at the moment we simply create a context for an empty request and go from there, like this:

    SimpleWorkerRequest request = new SimpleWorkerRequest("","","", null, new StringWriter());
    HttpContext context = new HttpContext(request);
    

    Then in the unit test initialize or in the unit test itself (before you create expected) you can set the current HttpContext as follows:

    HttpContext.Current = context;
    

    Then simply flesh out the fake context and possible fake sessionstate, etc as required.

    (Edit: This is all in VS2008, framework 3.5 by the way).

提交回复
热议问题