c# – 无法等待的模拟对象上的异步回调

c# – 无法等待的模拟对象上的异步回调,第1张

概述我试图模拟单元测试的复杂情况: _mockController = new Mock<IController>();_mockController.Setup(tc => tc.Interrupt(It.IsAny<Func<Task>>())) .Callback<Func<Task>>(async f => await f.Invoke()); IController有一个void方 我试图模拟单元测试的复杂情况:
_mockController = new Mock<IController>();_mockController.Setup(tc => tc.Interrupt(It.IsAny<Func<Task>>()))    .Callback<Func<Task>>(async f => await f.Invoke());

IController有一个voID方法Interrupt(Func< Task>> f),它将一些工作排​​队.

我正在测试的对象会调用Interrupt(),我可以像这样验证调用:

_mockController.Verify(tc => tc.Interrupt(It.IsAny<Func<Task>>()),Times.Once);

…但是当参数Func< Task>在回调中调用,在任务中不遵守await关键字:在任务完成之前继续执行测试(尽管在回调中等待).其中一个症状是在Interrupt()Task参数中添加await Task.Delay(1000)会将通过的测试转换为失败的测试.

这种行为是由于测试期间如何处理线程或任务的细微差别?或Moq的限制?我的测试方法有这个签名:

[Test]public async Task Test_name(){}
解决方法 回调不能返回值,因此不应该用于执行异步代码(或需要返回值的同步代码).回调是一种“注入点”,您可以将其挂钩以检查传递给方法的参数,但不能修改它返回的内容.

如果你想要一个lambda模拟,你可以使用返回:

_mockController.Setup(tc => tc.Interrupt(It.IsAny<Func<Task>>()))    .Returns(async f => await f());

(我假设中断返回任务).

the execution of the test continues before the Task finishes (despite the await in the callback).

是的,因为Callback不能返回值,所以它总是输入为Action / Action< ...>,所以你的异步lambda最终是一个异步voID方法,有all the problems that brings(如我的MSDN文章中所述).

更新:

Interrupt() is actually a voID method: what it does is queue the function (the argument) until the IController can be bothered to stop what it is doing. Then it invokes the function–which returns a Task–and awaits that Task

你可以模仿那种行为,如果这样可行的话:

List<Func<Task>> queue = new List<Func<Task>>();_mockController.Setup(tc => tc.Interrupt(It.IsAny<Func<Task>>()))    .Callback<Func<Task>>(f => queue.Add(f));... // Code that calls Interrupt// Start all queued tasks and wait for them to complete.await Task.WhenAll(queue.Select(f => f()));... // Assert / Verify
总结

以上是内存溢出为你收集整理的c# – 无法等待的模拟对象上的异步回调全部内容,希望文章能够帮你解决c# – 无法等待的模拟对象上的异步回调所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1252372.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存