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

时间:2018-01-24 22:52:16

标签: c# unit-testing nsubstitute objectcache

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

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

当我进入我的代码时

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

我正在使用Nsubstitute来模拟库。

有谁知道如何克服这个问题?

1 个答案:

答案 0 :(得分:4)

您需要为Get方法配置模拟。请参阅NSubstitute docsreturn方法的示例。

像...一样的东西。

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