假设我们如下代码调用了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. php--php设计模式留存

    装饰者模式 <?php interface Decorator { public function display(); } class XiaoFang implements Decorato ...

  2. 谈谈CSS3中display属性的Flex布局(弹性布局)

    最近为了微信小程序,回忆起之前有接触过的弹性布局(display:Flex) 网上摘录的原文地址:https://www.cnblogs.com/xuyuntao/articles/6391728.h ...

  3. 如何使你的PPT更高调

    PPT是什么? 俗话说的好,PPT就是"屁屁踢"/笑脸. PPT是微软office三件套之一,也就是演示文稿,用于演示(说了给没说一样). PPT的用途 视觉辅助 自动演示 阅读 ...

  4. undefined 和 not defined

    概念上的解释: undefined是javascript语言中定义的五个原始类中的一个,换句话说,undefined并不是程序报错,而是程序允许的一个值. not defined是javascript ...

  5. SpringBoot系列(八)分分钟学会Springboot多种解决跨域方式

    SpringBoot系列(八) 分分钟学会SpringBoot多种跨域解决方式 往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 s ...

  6. Python刷CSDN阅读数(仅供娱乐)

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ @File:csdn_reads.py @E-mail:3649427 ...

  7. Laravel 上手增删改查

    拿到一个框架,除了解框架,还要能实现基本的CURD操作. 添加 1.配置路由,指定添加页面: // routes/web.php 中增加如下: // 添加页面.存放路径 Laravel7/resour ...

  8. 高校战“疫”网络安全分享赛 Misc ez_mem&usb

    打开之后是一个流量包 用wireshark导出HTTP文件,有个upload,用一下binwalk,出来了一个镜像文件 用volatility搜一下,命令里有一个密码,看见了但是后来给忘了... 文件 ...

  9. [html][javascript] 关于SVG环形进度条

    下面是个例子: <style> .demo2{ transform-origin: center; transform: rotate(-90deg); transition: strok ...

  10. 《闲扯Redis五》List数据类型底层之quicklist

    一.前言 Redis 提供了5种数据类型:String(字符串).Hash(哈希).List(列表).Set(集合).Zset(有序集合),理解每种数据类型的特点对于redis的开发和运维非常重要. ...