MemoryCache.Testing is a mocking library that creates Microsoft.Extensions.Caching.Memory IMemoryCache system mocks. It's easy to use (usually just a single line of code) with implementations for both Moq and NSubstitute.
Start by creating a mocked memory cache using Create.MockedMemoryCache()
:
var cacheEntryKey = "SomethingInTheCache";
var expectedResult = Guid.NewGuid().ToString();
var mockedCache = Create.MockedMemoryCache();
var actualResult = mockedCache.GetOrCreate(cacheEntryKey, entry => expectedResult);
Assert.AreEqual(expectedResult, actualResult);
This creates a mocked IMemoryCache
. If your SUT populates the cache you're done. If it doesn't, or you like your arrange to be verbose, populate it as if you were using the real thing:
var cacheEntryKey = "SomethingInTheCache";
var expectedResult = Guid.NewGuid().ToString();
var mockedCache = Create.MockedMemoryCache();
mockedCache.Set(cacheEntryKey, expectedResult);
var actualResult = mockedCache.Get(cacheEntryKey);
Assert.AreEqual(expectedResult, actualResult);
The Moq implementation of Create.MockedMemoryCache()
returns the mocked memory cache. If you need the mock itself (e.g., to verify an invocation) use Mock.Get(mockedCache)
:
var cacheEntryKey = "SomethingInTheCache";
var expectedResult = Guid.NewGuid().ToString();
var mockedCache = Create.MockedMemoryCache();
var actualResult = mockedCache.GetOrCreate(cacheEntryKey, entry => expectedResult);
var cacheMock = Mock.Get(mockedCache);
cacheMock.Verify(x => x.CreateEntry(cacheEntryKey), Times.Once);
object cacheEntryValue;
cacheMock.Verify(x => x.TryGetValue(cacheEntryKey, out cacheEntryValue), Times.Once);
With regard to verifying invocations, all members of the IMemoryCache
interface are mocked.