I have a Cache class as follows:
public class MyCache : ICacheProvider
{
private readonly IMemoryCache _cache;
private readonly MemoryCacheOptions _options;
private readonly ILogger<InMemoryCacheProvider> _logger;
public MyCache(IMemoryCache cache, MemoryCacheOptions options, ILogger<InMemoryCacheProvider> logger)
{
//Elided
}
public virtual void Set<T>(string key, T value, TimeSpan expiration) where T : class
{
_cache.Set(key, value, expiration);
}
public virtual T Get<T>(string key) where T : class
{
if (_cache.Get(key) is T result)
{
return result;
}
return default(T);
}
// removed some code for clarity
}
ICacheProvider has methods like Set
and Get
.
Well how can I test this class? I need to test that set method actually sets something to the depencency. With FakeitEasy I have done the following:
[Fact]
public void SetTest()
{
var cache = A.Fake<MyCache>();
var item = A.Fake<TestClass>();
cache.Set("item", item);
A.CallTo(() => cache.Set("item", item)).MustHaveHappened();
}
But this didnt make too much sense to me.
What I am interested is, when I call the set method, I need to be able to check if there is really an object set in the fake cache or whatever. Same for Get and other methods.
Can you please elaborate?