lundi 11 juillet 2016

Mock IMemoryCache in unit test

I am using asp net core 1.0 and xunit.

I am trying to write a unit test for some code that uses IMemoryCache. However whenever I try to set a value in the IMemoryCache I get an Null reference error.

My unit test code is like this: The IMemoryCache is injected into the class I want to test. However when I try to set a value in the cache in the test I get a null reference.

public MyClass GetClassToTest()
{
    var mockCache = new Mock<IMemoryCache>();

    return new MyClass(mockCache.Object);
}

My question is...Do I need to setup the IMemoryCache somehow? Set a value for the DefaultValue? When IMemoryCache is Mocked what is the default value?

Thanks.

Aucun commentaire:

Enregistrer un commentaire