对包含HttpContext.Current.Cache的代码进行单元测试
2009-08-05 17:50 敏捷的水 阅读(3584) 评论(18) 编辑 收藏 举报假设我们如下代码调用了HttpContext.Current.Cache
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class CacheManager { public static HttpContext mHttpContext = HttpContext.Current; public void SetCache<t>( string key, T value) { mHttpContext.Cache.Insert(key, value, null , DateTime.MaxValue, new TimeSpan(0, 100, 0)); } public T GetCache<t>( string key) { return (T)mHttpContext.Cache.Get(key); } }</t></t> |
现在我有一个类调用了上面的GetCache<T>
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class LanguageController { private CacheManager cacheManger = new CacheManager(); public string Get_UserLanguage() { string userLanguage=cacheManger.GetCache< string >( "userLanguage" ); if (! string .IsNullOrEmpty(userLanguage)) return userLanguage; return "zh-CN" ; } }</ string > |
1 2 3 4 5 6 7 8 9 10 11 12 | [TestMethod] public void Test_Get_UserLanguage() { CacheManager cacheManger = new CacheManager(); cacheManger.SetCache< string >( "userLanguage" , "en-GB" ); LanguageController languageController = new LanguageController(); Assert.AreEqual(languageController.Get_UserLanguage(), "en-GB" ); }</ string > |
System.NullReferenceException: Object reference not set to an instance of an object.
跟踪调试,发现下面方法这句mHttpContext.Cache为空
1 2 3 4 | public void SetCache<t>( string key, T value) { mHttpContext.Cache.Insert(key, value, null , DateTime.MaxValue, new TimeSpan(0, 100, 0)); }</t> |
现在,将测试代码改为如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [TestMethod] public void Test_Get_Language_By_Fake() { HttpContext.Current = new HttpContext( new HttpRequest( null , "http://10.10.50.127/RGV2/devtest1" , null ), new HttpResponse( null )); CacheManager.mHttpContext = HttpContext.Current; CacheManager cacheManger = new CacheManager(); cacheManger.SetCache< string >( "userLanguage" , "en-GB" ); LanguageController languageController = new LanguageController(); Assert.AreEqual(languageController.Get_UserLanguage(), "en-GB" ); }</ string > |
测试通过:
总结,当我们测试的包含HttpContext.Current.Cache代码时:
1. 将HttpContext.Current.Cache 公布为类的静态属性,这样测试时,一个地方改了,全部就改过来了
2. 用下面的代码来给HttpContext.Current赋值
1 2 3 4 | HttpContext.Current = new HttpContext( new HttpRequest( null , "http://10.10.50.127/RGV2/devtest1" , null ), new HttpResponse( null )); CacheManager.mHttpContext = HttpContext.Current; |
3. 建议所有调用HttpContext获得值的地方,尽量公布为属性,这样方便测试,比如如下的代码我们就直接赋值了,这个和本文关系不大,只是一个实践而已。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class ConfigController { private string tempConfigPath; public string mConfigPath { get { if (tempConfigPath == null ) { tempConfigPath = HttpContext.Current.Server.MapPath( @"~/App_Data/config.xml" ); } return tempConfigPath; } set { tempConfigPath = value; } } } |
4. 我们也可以使用Mock,但是个人认为上面的方法更简单点。
扫码关注公众号,了解更多管理,见识,育儿等内容

作者: 王德水
出处:http://www.cnblogs.com/cnblogsfans
版权:本文版权归作者所有,转载需经作者同意。
出处:http://www.cnblogs.com/cnblogsfans
版权:本文版权归作者所有,转载需经作者同意。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?