Using httpcontext in unit test

后端 未结 4 825
终归单人心
终归单人心 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 15:09

    You could decorate your test method with the followings attributes:

    [TestMethod]
    [HostType("ASP.NET")]
    [UrlToTest("http://localhost:xxxx/")]
    [AspNetDevelopmentServerHost("$(SolutionDir)\\xxx\\", "/")]
    public void TestMethod()
    {
       ...
    }
    

    Then adding a Default.aspx file into your unit test proj.

    Inside the test method you can easily access to the HttpContext. If you want to debug, you may use some traces or interrupt the debugging execution with the instruction System.Diagnostics.Debugger.Break()

    public void TestMethod()
    {
       System.Diagnostics.Debugger.Break();
    
       ...
    }
    

    and then attaching debugger to the process as explained by MSDN: https://msdn.microsoft.com/en-us/library/vstudio/c6wf8e4z(v=vs.100).aspx

提交回复
热议问题