C# 单元测试 NSubstitute 无法在 ObjectCache 中设置值

C# Unit Test NSubstitute Unable to Set Value in ObjectCache

我无法在单元测试中使用 Set 在 ObjectCache 中插入缓存条目。

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

当我进入我的代码时

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

我正在使用 Nsubstitute 模拟库。

有谁知道如何解决这个问题?

您需要为 Get 方法配置 mock。请参阅 NSubstitute docs.

return 方法的示例

类似...

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