简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM