繁体   English   中英

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方法配置模拟。 请参阅NSubstitute docs中有关return方法的示例

就像是...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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