大佬教程收集整理的这篇文章主要介绍了.net – 如何使单元测试可以使用HttpContext?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我面临的问题是这个类的静态构造函数使用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不会为null,我可以成功设置它:
httpServerUtility server = httpContext.Current.Server;
例如,您可以调用该方法:
var wrapper = new httpContextWrapper(httpContext.Current); Foo.UploadedFile(wrapper);
在您的单元测试中(使用Rhino Mocks):
var contextmock = mockRepository.Generatemock<httpContextBase>(); // TODO: Define expectations on the mocked object Foo.UploadedFile(contextmock);
或者,如果您愿意,请使用Constructor Injection.
以上是大佬教程为你收集整理的.net – 如何使单元测试可以使用HttpContext?全部内容,希望文章能够帮你解决.net – 如何使单元测试可以使用HttpContext?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。