对包含HttpContext.Current.Cache的代码进行单元测试
假设我们如下代码调用了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>
|
我们现在需要测LanguageController的Get_UserLanuage,写如下代码
|
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/archive/2009/08/05/1539788.html如需转载请自行联系原作者
王德水
对包含HttpContext.Current.Cache的代码进行单元测试的更多相关文章
- Cache及(HttpRuntime.Cache与HttpContext.Current.Cache)
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/avon520/archive/2009/11/25/4872704.aspx .NET中Cache有两种调用方式:Ht ...
- 缓存 HttpContext.Current.Cache和HttpRuntime.Cache的区别
先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cache. 我们再用. ...
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cac ...
- HttpContext.Current.Cache 和 HttpRuntime.Cache 区别
原文地址:http://blog.csdn.net/avon520/article/details/4872704 .NET中Cache有两种调用方式:HttpContext.Current.Cach ...
- HttpContext.Current.Cache 过期时间
原文:HttpContext.Current.Cache 过期时间 为了更快的读取数据,我们一般会把常用到的数据加载到Cache中 在.NET中,Cache的存在可以依赖多中方式,主要用到HttpCo ...
- HttpContext.Current.Cache 和 HttpRuntime.Cache
HttpRuntime.Cache:用于winfrom 和 web HttpContext.Current.Cache 用于web .NET中Cache有两种调用方式:HttpContext.Curr ...
- Asp.net中的Cache--HttpRuntim.Cache 和 HttpContext.Current.Cache
在ASP.NET中有两个类都提供缓存支持, 一个是HttpRuntime类的Cache属性, 另一个是HttpContext类的Cache属性. 通过查看这两个属性的类型可以发现其实这两个属性都是Sy ...
- HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching
先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cac ...
- HttpContext.Current.Cache使用文件依赖问题
HttpContext.Current.Cache.Insert("FCacheMs", tb, New CacheDependency(HttpContext.Current.S ...
随机推荐
- 中阶 d05 tomcat 安装 eclipse上配置tomcat
eclipse使用参考 https://www.bilibili.com/video/av49438855/?p=24 1. 直接解压 ,然后找到bin/startup.bat 2. 可以安装 启动之 ...
- 萌新带你开车上p站(三)
本文作者:萌新 前情回顾: 萌新带你开车上p站(一) 萌新带你开车上p站(二) 0x08 题目给的提示是和运算符优先级有关 登录后直接看源码 mistake@pwnable:~$ ls flag mi ...
- 内存对齐 align
/* 地址对齐:指的是存放数据的首地址%N==0,而且整个结构体的大小%M(结构体的有效对齐值)==0 1 数据类型的自身对齐值:char:1 short:2 int,flolat,double:4 ...
- Java基础】并发 - 多线程
Java基础]并发 - 多线程 分类: Java2014-05-03 23:56 275人阅读 评论(0) 收藏 举报 Java 目录(?)[+] 介绍 Java多线程 多线程任务执行 大多数 ...
- Wpf之HandyControls与MaterialDesign混用之DataGrid
首先在App.Xaml引入相关资源 <Application.Resources> <ResourceDictionary> <ResourceDictionary.Me ...
- Web三维编程入门总结之三:3D碰撞检测初探
自己动手写一个方法比分析他人的写的方法困难很多,由此而来的对程序的进一步理解也是分析别人的代码很难得到的. 一.先来几张效果图: 1.场景中有两个半径为1的球体,蓝色线段从球心出发指向球体的“正向” ...
- GeiGebra指令
- 疲劳驾驶打瞌睡?python保障您的驾驶安全
道路千万条,安全第一条!疲劳驾驶可谓交通事故几大罪魁祸首之一,根据美国一项研究显示,司机睡眠不足4小时,交通事故肇事几率等同于醉驾. 为了减少疲劳驾驶现象,驾驶员疲劳检测应运而生.这是一项安全技术,可 ...
- day7作业
# day7作业 # 1. 使用while循环输出1 2 3 4 5 6 8 9 10 count = 1 while count < 11: if count == 7: count += 1 ...
- D - Free Candies UVA - 10118
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...