假设我们如下代码调用了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的代码进行单元测试的更多相关文章

  1. Cache及(HttpRuntime.Cache与HttpContext.Current.Cache)

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/avon520/archive/2009/11/25/4872704.aspx .NET中Cache有两种调用方式:Ht ...

  2. 缓存 HttpContext.Current.Cache和HttpRuntime.Cache的区别

    先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cache.  我们再用. ...

  3. HttpContext.Current.Cache 和HttpRuntime.Cache的区别

    先看MSDN上的解释:      HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象.      HttpRuntime.Cache:获取当前应用程序的Cac ...

  4. HttpContext.Current.Cache 和 HttpRuntime.Cache 区别

    原文地址:http://blog.csdn.net/avon520/article/details/4872704 .NET中Cache有两种调用方式:HttpContext.Current.Cach ...

  5. HttpContext.Current.Cache 过期时间

    原文:HttpContext.Current.Cache 过期时间 为了更快的读取数据,我们一般会把常用到的数据加载到Cache中 在.NET中,Cache的存在可以依赖多中方式,主要用到HttpCo ...

  6. HttpContext.Current.Cache 和 HttpRuntime.Cache

    HttpRuntime.Cache:用于winfrom 和 web HttpContext.Current.Cache 用于web .NET中Cache有两种调用方式:HttpContext.Curr ...

  7. Asp.net中的Cache--HttpRuntim.Cache 和 HttpContext.Current.Cache

    在ASP.NET中有两个类都提供缓存支持, 一个是HttpRuntime类的Cache属性, 另一个是HttpContext类的Cache属性. 通过查看这两个属性的类型可以发现其实这两个属性都是Sy ...

  8. HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching

    先看MSDN上的解释:      HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象.      HttpRuntime.Cache:获取当前应用程序的Cac ...

  9. HttpContext.Current.Cache使用文件依赖问题

    HttpContext.Current.Cache.Insert("FCacheMs", tb, New CacheDependency(HttpContext.Current.S ...

随机推荐

  1. html的嵌套规则

    html元素分为块状元素和内联元素,块状元素作为其他元素的容器. 块状元素可以嵌套其他块状元素 块状元素可以嵌套内联元素 内联元素不能嵌套块状元素 p内不能嵌套块状元素 有几个特殊块状元素只能包含内联 ...

  2. NullPointerException的处理新方式,Java14真的太香了

    在Java语言中,处理空指针往往是一件很头疼的事情,一不小心,说不定就搞出个线上Bug,让你的绩效考核拿到3.25.最近新出的Java14,相信大家都有所耳闻,那么今天就来看看,面对NullPoint ...

  3. JAVA中String和StringBuilder类的特点及使用

    转自:https://www.imooc.com/code/2202 仅做个人学习记录之用,侵删! 什么是 Java 中的字符串 在 Java 中,字符串被作为 String 类型的对象处理. Str ...

  4. redis 练习 a的数据库数据迁移到b数据库

    思路 1.从a redis中获取所有的key 2.判断key的类型 3.根据key的类型,判断使用的是set/hset类型 4.set到b redis中(写入到b redis中)

  5. 力软敏捷框架集成布局插件(ce-layout)

    最近用力软的框架觉得框架在布局这块不是很友好特别是对像css不是很好的程序员来说,大部分大家都是后端程序员. 所以决定集成一个和力软敏捷框架风格比较一致的布局插件进来 插件ce-layout ,下载地 ...

  6. OO第三单元作业分析

    一.JML的理论基础应用工具链 JML是用于对Java程序进行规格化设计的一种表示语言.基于Larch方法构建. (1)注释 JML以javadoc注释的方式来表示规格,每行都以@起头.有两种注释方式 ...

  7. qt creator源码全方面分析(4-0)

    Qt系统 Qt Creator源码是在Qt对象和框架基础下写的,因此,阅读Qt Creator源码,你首先对Qt得有一定的了解. Qt Core Qt Core特征: The Meta-Object ...

  8. 1年左右的Java开发经验面试者的心得

    面试,相信只要踏入这行业的人都会经历,不同的公司有不同的面试流程,但是综合起来,其实还是大体一致的!只有不断的总结自己的面试经历,得出自己的技术不足点,才能更好的去查缺补漏,从而更加自信的进行面试找到 ...

  9. 程序员小张的第一篇博文 --记Markdown的使用学习

    1.前言 为了即将到来的面试做准备,以及记录一下平日里自己的学习过程和生活日常,我开始进驻博客园啦!这就是我的第一篇博客(有点小激动)~ 作为一只新手,首先记录一下今晚的编写博文的学习过程吧~ 2.使 ...

  10. 给我Python几十行代码,我还你一个微信聊天助手

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 故事胶片 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...