这个问题在这里已经有了答案:
How can I tell Moq to return a Task?
(4 个回答)
5年前关闭。
我正在尝试使用 Moq 来测试 WebAPI Controller 和 Redis 数据库之间的集成,使用 StackExchange.Redis 客户端,但无法弄清楚如何在包含回调或其他断言的模拟异步方法上设置可验证的期望行为。
通常,我会使用以下语法:
const string KEY = "some_key";
var db = new Mock<IDatabase>();
db.Setup(d => d.HashSetAsync(KEY, It.IsAny<HashEntry[]>(),It.IsAny<CommandFlags>()))
.Callback<RedisKey,HashEntry[],CommandFlags>((key, hash, flags) => {
hash.ShouldContain(entry => entry.Name == "customerid");
hash.ShouldContain(entry => entry.Name == "quotenumber");
})
.Verifiable();
但这给了我:
'Moq.Language.Flow.IReturnsThrows<StackExchange.Redis.IDatabase,System.Threading.Tasks.Task>' does not contain a definition for 'Verifiable' and no extension method 'Verifiable' accepting a first argument of type 'Moq.Language.Flow.IReturnsThrows' could be found (are you missing a using directive or an assembly reference?)
如果我改变
db.HashSetAsync
至 db.HashSet
在 Setup
调用,它按预期工作。似乎在常规方法上设置回调返回 ICallbackResult
但是在异步方法调用上设置回调会返回 IReturnsThrows
- 我不确定你是如何将其中一个标记为可验证的。有任何想法吗?
最佳答案
对于异步方法,您需要返回一个完整的 Task
从使用回调之前的设置
看看这里:
Using Moq to mock an asynchronous method for a unit test
You're creating a task but never starting it, so it's never completing. However, don't just start the task - instead, change to using
Task.FromResult<TResult>
which will give you a task which has already completed:
这有效
const string KEY = "some_key";
var db = new Mock<IDatabase>();
db.Setup(d => d.HashSetAsync(KEY, It.IsAny<HashEntry[]>(), It.IsAny<CommandFlags>()))
.Returns(Task.FromResult<object>(null))
.Callback<RedisKey, HashEntry[], CommandFlags>((key, hash, flags) => {
hash.ShouldContain(entry => entry.Name == "customerid");
hash.ShouldContain(entry => entry.Name == "quotenumber");
})
.Verifiable();
关于async-await - 如何在模拟的异步方法上设置可验证的期望?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36428740/