我有一个服务层,其中包含多个方法。这些方法已经实现了缓存,例如以下代码:
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");
}
问题是,在单元测试期间,我的CacheHandler中的HttpContext.Current为空(显然)。
最简单的解决方法是什么?
(请尽可能具体,因为我以前没有做过很多单元测试)