假设我们如下代码调用了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. vue-shop项目第二天(用于个人学习的记录)

    vue-shop项目第二天 1.实现路由导航守卫功能. router.beforeEach((to, from, next) => { // to 将要访问的路径 from 代表从哪个路径跳转而 ...

  2. Java第十二天,权限修饰符

    Java当中权限修饰符共有四种.分别是public.protected.(default).private. 注:YSE代表可访问,NO代表不可访问.   同一个类 同一个包,非继承 不同的包,有继承 ...

  3. JAVA中的==和equals()的区别

    一.先来说说Java的基本数据类型和引用类型 八大基本数据类型:Byte,short,int,long,double,folat,boolean,char,其中占一个字节的是byte,short和ch ...

  4. 如何用 Python 绘制玫瑰图等常见疫情图

    新冠疫情已经持续好几个月了,目前,我国疫情已经基本控制住了,而欧美国家正处于爆发期,我们会看到很多网站都提供了多种疫情统计图,今天我们使用 Python 的 pyecharts 框架来绘制一些比较常见 ...

  5. Delphi TMemo 可以显示、编辑多行文本

    多行编辑框组件(TMemo)TMemo组件可以显示.编辑多行文本,是一个标准的Windows多行编辑组件.对一些比较多的文本内容可以利用TMemo组件来显示.编辑. 1.TMemo组件的典型用法 TM ...

  6. numpy basic sheatsheet

    NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库.NumPy 通常与 SciPy(Scien ...

  7. Alpha Release Note 12/15/2015

    内容提要: ******Personal Photo Experience可供您存放所有的私人照片,系统会自动整理内容,您可以借助搜索功能快速找到所需图片,同时过滤重复图片和低质量图片,给您全新的搜索 ...

  8. Daily Scrum 12/14/2015

    Progress: Dong&Minlong: 基于Oxford Speech API成功实现语音输入的功能,但由于服务器存在访问次数的限制(每分钟6次),所以暂不准备将此功能加入ALPHA版 ...

  9. 初始化 RESTful API 风格的博客系统

    作者:HelloGitHub-追梦人物 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 在 HelloDjango 全栈系列教程的第一步--Django博客教程(第二版)中 ...

  10. Java 多线程 -- 理解锁:手动实现可重入锁和不可重入锁

    JDK提供的大多数内置锁都是可重入的,也就是 说,如果某个线程试图获取一个已经由它自己持有的锁时,那么这个请求会立 刻成功,并且会将这个锁的计数值加1,而当线程退出同步代码块时,计数器 将会递减,当计 ...