对包含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 ...
随机推荐
- Shell:Day04.笔记
grep与正则表达式: 1.grep程序 Linux下有文本处理三剑客 - - grep sed awk grep:文本 行 过滤工具 sed:文本 行 编辑器(流编辑器) awk:报告生成器(做文本 ...
- readthedocs网托管持多语言文档
希望在readthedocs上创建支持多语言的文档,效果类似: 通过语言选项,可以切到到不同的语言版本:实现这个目标包含两个主要步骤: 在本地对文档进行翻译 在readthedocs.org上配置翻译 ...
- 【翻译】OpenVINO Pre-Trained 预训练模型介绍
OpenVINO 系列软件包预训练模型介绍 本文翻译自 Intel OpenVINO 的 "Overview of OpenVINO Toolkit Pre-Trained Models& ...
- buuctf misc wp 02
buuctf misc wp 02 7.LSB 8.乌镇峰会种图 9.rar 10.qr 11.ningen 12.文件中的秘密 13.wireshark 14.镜子里面的世界 15.小明的保险箱 1 ...
- shell-function 删除目录和文件
function sDelDirFile() { if [ "$#" -eq 1 ];then if [ -e "$1" ];then rm "$1& ...
- sqlserver2008 服务器实例连接
一.sqlserver 配置管理器里面,看sqlserver服务那个,如果只有一个mssqlserver,那就用local或者.来访问,如果不是,可能有命名实例.
- 带你五分钟了解python的函数式编程与闭包
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:梁唐 PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行 ...
- 玩转控件:Fucking ERP之流程图
前言 首先,跟守护在作者公众号和私信作者催更的朋友们道个歉.疫情的原因,公司从年初到现在一直处于996+的高压模式,导致公众号更新频率较低.而且作者每更新一篇原创公众号,既要对自己沉淀知识负责,也要对 ...
- Dom节点操作总结
Dom 一:Dom的概念 Dom的简介: 全称为 document object model 文档对象模型,是操作文档的一整套方法 - 文档 - html,document时一个对象,是dom ...
- Matlab学习-(1)
1. 认识Matlab (1)MATLAB是美国MathWorks公司出品的商业数学软件,用于算法开发.数据可视化.数据分析以及数值计算的高级技术计算语言和交互式环境,主要包括MATLAB和Simul ...