对包含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基础;Day07
dns服务 dns的作用:地址解析 IP -> 域名(反向) 域名 -> IP(正向) 类型 主域名服务器 负责维护一个区域的所有域名信息,是特定的所有信息的权威信息源,数据可以修改. ...
- Centos7.x & RedHat7.x系统忘记 root 密码解决办法
重启系统进入引导页面 先将机器重启 根据提示按下e进入内核编辑页面 找到linux16参数行,并在行尾加上rd.break,之后按下Ctrl+X重启 如上图所示,重启之后将进入救援模式. 这是依次输入 ...
- (js描述的)数据结构[树结构1.2](12)
1.先序遍历 2.中序遍历 3.后序遍历 4.递归调用栈详解: 详细见: https://zhuanlan.zhihu.com/p/24291978 5.删除节点操作分析: 5.代码封装 //封装二叉 ...
- 2017蓝桥杯取位数(C++B组)
题目: 标题:取数位求1个整数的第k位数字有很多种方法.以下的方法就是一种.// 求x用10进制表示时的数位长度 int len(int x){ if(x<10) return 1; retur ...
- 中阶d03.2 JDBC联合properties使用,通过读取本地配置文件为代码传递参数
* 使用properties读取本地配置文件为代码传递参数 * url.用户名.密码.驱动地址等配置可以在配置文件中使用 main package zj_1_JDBC.properties; impo ...
- 讲讲HashMap的理解,以及HashMap在1.7和1.8版本的变化(2020/4/16)
HashMap的适用场景,作用,优缺点
- Camunda 流程引擎的一种 Adapter 层实现
上一篇说明了选择 Camunda 的理由.这一篇说明如何实现适配层. 当前还没有专门写一篇对 Camunda 各个功能的详细介绍.如果要获得比较直观的感受,可以下载 Modeler 或者使用在线版的 ...
- python实现服务器监控报警消息用微信发送(附代码)
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:NicePython PS:如有需要Python学习资料的小伙伴可以加 ...
- Python 分析后告诉你闲鱼上哪些商品抢手?
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:[Airpython] PS:如有需要Python学习资料的小伙伴可以 ...
- O - Employment Planning HDU - 1158
题目大意: 第一行一个n,表示共n个月份,然后第二行分别表示一个工人的聘请工资,月薪水,解雇工资.第三行是n个月每个月需要的工人的最少数目.然后求最少花费 题解: dp[i][j] 表示第i个月聘请j ...