对包含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 ...
随机推荐
- Linux Shell编程,使用随机数
Shell有一个$RANDOM环境变量,读取它可以获得5位随机数 在/dev下,有两个字符设备/dev/random和/dev/urandom,可以产生二进制随机数 其中,urandom为非阻塞随机数 ...
- Java第十五天,泛型
一.定义 泛型是一种未知的数据类型,即当我们不知道该使用哪种数据类型的时候,可以使用泛型. 泛型的本质是为了 参数化 类型(在不创建新的类型的情况下,通过泛型指定的不同类型来控制形参具体限制的类型) ...
- WEB页面实现方法
页面分类 :添加页.修改页.列表页.详情页.功能页.删除 一.添加 1) 准备tpl.action(添加页.添加页保存公用一个action),并确认是否登录才显示2) 书写添加页action代码,例如 ...
- <context:component-scan base-package=""> 与 <context:annotation-config 区别
<context:component-scan base-package=""> <context:annotation-config (2012-11-16 2 ...
- 带权值的LCA
例题:http://poj.org/problem?id=1986 POJ1986 Distance Queries Language: Default Distance Queries Time L ...
- 一道简单的SQL注入题
这是我真正意义上来说做的第一道SQL题目,感觉从这个题目里还是能学到好多东西的,这里记录一下这个题目的writeup和在其中学到的东西 link:https://www.ichunqiu.com/ba ...
- 【题解】P3349 [ZJOI2016]小星星 - 子集dp - 容斥
P3349 [ZJOI2016]小星星 声明:本博客所有题解都参照了网络资料或其他博客,仅为博主想加深理解而写,如有疑问欢迎与博主讨论✧。٩(ˊᗜˋ)و✧*。 题目描述 小 \(Y\) 是一个心灵手巧 ...
- python之实现图像的手绘效果
https://blog.csdn.net/riba2534/article/details/74152285 原图: b: c: d: 最终图:
- javascript-网页尺寸
scrollWidth:对象的实际内容的宽度,不包边线宽度,会随对象中内容超过可视区后而变大. clientWidth:对象内容的可视区的宽度,不包滚动条等边线,会随对象显示大小的变化而改变. off ...
- mac使用brew安装mysql5.7
安装mysql5.7 brew install mysql@5.7 设置环境变量(可能安装完自动生成过了,可以cat ~/.zshrc看一下,有了就不用添加了 ) echo 'export PATH= ...