I am writing a few unit tests and wondering if it is beneficial to mock Cache , and if so, how?
Currently, in my tests, I mock the HttpContextBase and wrap it in a custom HttpContextFactory :
var mockedHttpContextBase = new Mock<HttpContextBase>(); IHttpContextFactory httpContextFactory = new HttpContextFactory { Current = mockedHttpContextBase.Object };
and when my code consumes an IHttpContextFactory , I check if the cache is zero before doing anything with it.
var cache = _httpContextFactory.Current.Cache; Func<SomeReturnType> doSomeWork = () => _foo.someMethodIExecute(param1,param2); return cache != null ? cache.GetOrStore("doSomeWorkCacheKey",doSomeWork, 900) : doSomeWork.Invoke();
Is it correct to check that the cache is zero, as it is every time I use it, or would you mock the cache also in the test so that it is not zero when performing unit tests?
Jamie dixon
source share