Search code examples
c#unit-testingnsubstituteobjectcache

C# Unit Test NSubstitute Unable to Set Value in ObjectCache


I am unable to insert a cache entry in ObjectCache with Set in my unit tests.

var objectCache = Substitute.For<ObjectCache>();
objectCache.Set("TestKey", object, Arg.Any<DateTimeOffset>());

When I step into my code

var cachedData = _objectCache.Get("TestKey");
// cachedData is null

I am using Nsubstitute for mocking libraries.

Does anyone know how to overcome this issue?


Solution

  • You will need to configure the mock for the Get method. See the example for the return method in the NSubstitute docs.

    Something like...

    var objectCache = Substitute.For<ObjectCache>();
    objectCache.Get("TestKey").Returns(...what you want it to return);