How to do Mock IMemoryCache c #

0

I'm trying to test a class that implements IMemoryCache (IMemoryCache contains static methods which can not be mocados).

This is what I tried to do:

var teste = new Mock<IMemoryCache>();
mockCache.Setup(m => m.Set(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MemoryCacheEntryOptions>())).Verifiable();

If I only pass Mock to the class I want to test, without setting the Set method, it creates the correct instance, but it fails to create a cache.

The constructor of the class I want to test looks like this:

public class ClasseASerTestada
{
   private readyonly IMemoryCache _memoryCache;

   public ClasseASerTestada(IMemoryCache memorycache)
   {
      _memoryCache = memoryCache;  
   }
}

The error gives exactly here:

meuMetodoDaClasseAcima()
{
  ....
  _memoryCache.Set("", "", new TimeSpan(0, 0, 0, 60);
}

Does anyone have any ideas?

Thanks!

    
asked by anonymous 06.05.2018 / 13:23

0 answers