对包含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 ...
随机推荐
- spring08
这里主要学习的是关于spring之后中与ioc不同的aop技术:面向切面编程是spring基石之一: 解决代码混乱文体,代码分散,当部分修改时,要逐个修改当更多的日志以及验证介入之后会使代码变得更加的 ...
- editplus 怎么替换为换行
到editplus 的搜索 菜单中,选择替换,记住 这边如果是简单的一些 通用字符 替换可以直接替换,如果是一些特殊的字符 那必须选择 替换框左下中间的 “正则表达式”,即把这个“正则表达式” 前边的 ...
- list[列表]的使用
#!/usr/bin/env python3# -*- coding:utf-8 -*-# name:zzyushop_list = [["手机",5000], ["电脑 ...
- AJ学IOS 之微博项目实战(12)发送微博自定义工具条代理实现点击事件
AJ分享,必须精品 一:效果 二:封装好的工具条 NYComposeToolbar.h 带代理方法 #import <UIKit/UIKit.h> typedef enum { NYCom ...
- 解决SpringMVC的乱码问题:CharacterEncodingFilter
在使用 SpringMVC 框架的过程中,如果前台有包含中文的请求,或者后台有包含中文的响应,有可能会出现乱码的情况.在以前的 Servlet 中,我们使用 request.setCharacterE ...
- springboot集成swagger2多模块中文配置详细步骤,解决集成mybatis或mybatis-plus无法正常使用问题
pom.xm里写入swagger依赖: <dependency> <groupId>io.springfox</groupId> <artifactId> ...
- Redis Linux安装+配置
1.进入指定目录,下载资源(也可本地下载后复制到指定目录) wget http://download.redis.io/releases/redis-5.0.5.tar.gz 2.解压到指定目录 ta ...
- jquary 动画j
1) 点击 id为d1的正方体,将其后所有class为div1的正方体背景色设置为绿色. 代码如下: <div class="div1" > </di ...
- 排序1 - 选择排序 & 插入排序
请原谅我没有按照之前图片的分类来介绍排序算法,先说最简单的两种排序算法(冒泡略过),选择排序和插入排序,之前老是容易记混.默认输出升序的序列啊,哈哈. 选择排序 对于输入长度为n的数组,一共比较n-1 ...
- 吃瓜的正确姿势,Python绘制罗志祥词云图
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 这篇文章中向大家介绍了Python绘制词云的方法,不难看出绘制词云可以说是一 ...