写Web单元测试,由于以前的DLL大量引用有HttpContext.Current的代码,结果每次运行到这类代码都会由于HttpContext.Current==null导致出错。可以在单元测试代码里直接给HttpContext.Current赋值达到回避这种错误的效果。代码如下:
HttpContext.Current = new HttpContext(new HttpRequest("", "http://localhost", ""), new HttpResponse(new StringWriter(new StringBuilder())));