【发布时间】:2020-07-04 01:06:19
【问题描述】:
我正在测试的方法中有这段代码:
var devices = await _cache.GetOrAddAsync(_cacheKey, AddItemFactory, DateTimeOffset.Now.AddMinutes(Configuration.DeviceInMinutes));`
其中_cache 是IAppCache,在测试中被new Mock<IAppCache>(); 模拟
AddItemFactory 具有以下签名:
private async Task<IDictionary<int, Device>> AddItemFactory()
在我的测试中我写过:
_appCache.Setup(x => x.GetOrAddAsync(
It.IsAny<string>(),
It.IsAny<Func<Task<IDictionary<int, Device>>>>(),
It.IsAny<DateTimeOffset>())
).ReturnsAsync(fakeDictionary);
当 .Setup 被评估时,我的测试崩溃并出现以下错误:
System.NotSupportedException : Invalid setup on an extension method: x => x.GetOrAddAsync<IDictionary<int, Device>>(It.IsAny<string>(), It.IsAny<Func<Task<IDictionary<int, Device>>>>(), It.IsAny<DateTimeOffset>())
我认为它被正确地模拟了,因为 Visual Studio 智能感知没有抱怨和签名错误,但我不明白发生了什么。
【问题讨论】:
-
IAppCache.GetOrAddAsync的签名是什么? -
这能回答你的问题吗? Mocking Extension Methods with Moq
-
@mm8 这是我在 _appCache.Setup 中的内容。 (字符串, Func
>, DateTimeOffset) -
@RoboKozo:如果您需要任何帮助,您需要提供更多详细信息。根据您问题中的信息,您的问题无法重现。
-
嗯...好吧,我还可以告知该应用正在使用github.com/alastairtree/LazyCache