对包含HttpContext.Current.Cache的代码进行单元测试

假设我们如下代码调用了HttpContext.Current.Cache

public class CacheManager
{
  public static HttpContext mHttpContext = HttpContext.Current;

  public void SetCache(string key, T value)
  {
     mHttpContext.Cache.Insert(key, value, null, DateTime.MaxValue, new TimeSpan(0, 100, 0));
  }

  public T GetCache(string key)
  {
     return (T)mHttpContext.Cache.Get(key);
  }

}
现在我有一个类调用了上面的GetCache<T>
public class LanguageController
{
    private CacheManager cacheManger = new CacheManager();
  
    public string Get_UserLanguage()
    {
        string userLanguage=cacheManger.GetCache("userLanguage");

        if (!string.IsNullOrEmpty(userLanguage)) return userLanguage;

        return "zh-CN";
    }        
}
我们现在需要测LanguageController的Get_UserLanuage,写如下代码

[TestMethod]
public void Test_Get_UserLanguage()
{

  CacheManager cacheManger = new CacheManager();
  cacheManger.SetCache("userLanguage", "en-GB");

  LanguageController languageController = new LanguageController();

  Assert.AreEqual(languageController.Get_UserLanguage(), "en-GB");

 }
运行测试,失败,得到如下消息

System.NullReferenceException: Object reference not set to an instance of an object.

跟踪调试,发现下面方法这句mHttpContext.Cache为空

public void SetCache(string key, T value)
{
   mHttpContext.Cache.Insert(key, value, null, DateTime.MaxValue, new TimeSpan(0, 100, 0));
}
现在,将测试代码改为如下:
[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("userLanguage", "en-GB");

  LanguageController languageController = new LanguageController();

  Assert.AreEqual(languageController.Get_UserLanguage(), "en-GB");

}
测试通过:
 
总结,当我们测试的包含HttpContext.Current.Cache代码时:
1. 将HttpContext.Current.Cache 公布为类的静态属性,这样测试时,一个地方改了,全部就改过来了
2. 用下面的代码来给HttpContext.Current赋值
HttpContext.Current = new HttpContext(new HttpRequest(null, 
                        "http://10.10.50.127/RGV2/devtest1", null), new HttpResponse(null));

CacheManager.mHttpContext = HttpContext.Current;
3. 建议所有调用HttpContext获得值的地方,尽量公布为属性,这样方便测试,比如如下的代码我们就直接赋值了,这个和本文关系不大,只是一个实践而已。
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,但是个人认为上面的方法更简单点。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值