C# Unit Test NSubstitute Unable to Set Value in ObjectCache

950 Views Asked by At

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?

1

There are 1 best solutions below

0
Matt Stannett On BEST ANSWER

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);