简体   繁体   English

最小起订量验证超时

[英]Moq verify timeout

I have some production code which runs asynchronously (actually an actor system).我有一些异步运行的生产代码(实际上是一个演员系统)。 The following test checks whether some interface is properly called if a certain event occurs.下面的测试检查在某个事件发生时是否正确调用了某个接口。 It currently reads:目前是这样写的:

var restarts = new Mock<ISystemCommandService>();
restarts.Setup(c => c.CommandType()).Returns("RestartCommand");
var subject = CreateSubject(restarts.Object);
subject.Tell(new Event ("TestEventName", "ValueChanged"));
Thread.Sleep(millisecondsTimeout: 300);
restarts.Verify(r => r.CommandAsync(It.Is<string>(s => 
    s.Contains(@"""RestartBackend"":true"))));

The test is green and all is well, but I'm unhappy about the Thread.Sleep(300) part.测试是绿色的,一切都很好,但我对 Thread.Sleep(300) 部分不满意。 I can't remove it because the actor system needs some time to process the message, but the 300 ms are hardware dependant.我无法删除它,因为演员系统需要一些时间来处理消息,但 300 毫秒取决于硬件。 It would be great if I could delete the Sleep() and write如果我可以删除 Sleep() 并写入,那就太好了

restarts.Verify(r => r.CommandAsync((It.Is<string>(s => 
        s.Contains(@"""RestartBackend"":true"))),
        TimeSpan.FromMilliseconds(500));

and Verify would wait up to 500 ms before it fails.并且 Verify 会在失败前最多等待 500 毫秒。 Or is there another way?或者还有其他方法吗?

Ok, the answer is https://github.com/sekskant/AsyncVerifyForMoq .好的,答案是https://github.com/sekskant/AsyncVerifyForMoq This contains an extension method for Mock, which provides an async verify with timeout.这包含 Mock 的扩展方法,它提供带超时的异步验证。 I removed the Sleep() and the last line now reads:我删除了 Sleep(),最后一行现在是:

await restarts.AsyncVerify(r => r.CommandAsync(It.Is<string>(s =>
    s.Contains(@"""RestartBackend"":true"))), 
    Times.Once(), 
    TimeSpan.FromSeconds(3));

It's actually 100 ms slower than using Sleep() on my development machine, but I don't care, this won't fail even on the slowest hardware.它实际上比在我的开发机器上使用 Sleep() 慢 100 毫秒,但我不在乎,即使在最慢的硬件上也不会失败。 Thanks!谢谢!

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

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