【发布时间】:2011-03-29 08:49:30
【问题描述】:
我想编写一个单元测试来测试一个名为 UploadedFile 的类的功能。
我面临的问题是这个类的静态构造函数使用 HttpContext.Current 属性,并且因为我正在从类库运行我的单元测试,所以在测试时我没有 HttpContext。
看看我的静态构造函数:
static UploadedFile()
{
if (HttpContext.Current == null)
throw new Exception("web server not available");
HttpServerUtility server = HttpContext.Current.Server;
// SET UploadedFileMappingFile Names:
_resourceFileNames = new StringDictionary();
_resourceFileNames[_suppoertedFileStructures] = server.MapPath(SupportedUploadedFileStructures);
_resourceFileNames[_supportedFileStructuresXSD] = server.MapPath(SupportedUploadedFileStructuresXSD);
_resourceFileNames[UploadedFileEnum.UploadedFileFormatENUM.CSV.ToString()] = server.MapPath(UploadedFileColumnMap);
}
我应该在我的测试环境中做什么,以使HttpContext.Current 不会为空并且我可以成功设置:
HttpServerUtility server = HttpContext.Current.Server;
【问题讨论】:
标签: .net asp.net unit-testing httpcontext httpserverutility