我有一个服务层,它有一系列的方法。这些方法已经实现缓存,如下所示:如何对使用缓存的服务进行单元测试?
string key = "GetCategories";
if (CacheHandler.IsCachingEnabled() && !CacheHandler.ContainsKey(key))
{
var categories = RequestHelper.MakeRequest("get_category_index")["categories"];
var converted = categories.ToObject<List<Category>>();
CacheHandler.InsertToCache(key,converted);
return converted;
}
return CacheHandler.GetCache(key) as List<Category>;
现在,问题是我也要打个单元测试,如下所示:
[TestMethod]
public void GetCategories()
{
IContentService contentService = new ContentService();
var resp = contentService.GetCategories();
Assert.IsNotNull(resp,"Should not be null");
}
问题是,该HttpContext.Current
在我的CacheHandler
在单元测试期间显然是空的。
解决此问题的最简单方法是什么?
它的工作!完善!这真的帮助我马上:)犀牛嘲笑也非常好。谢谢 –