最小起订量 - 如果类吞下异常,如何验证方法尚未被调用

发布于 2024-08-04 23:29:24 字数 323 浏览 4 评论 0原文

我正在尝试使用 Moq 测试一个相当复杂的类,但遇到了问题。

我试图验证一个方法不会被调用,通常通过设置 MockBehavior.Strict 可以很简单地做到这一点,但这里该类有自己的错误报告机制,因此它会吞掉 Moq 抛出的异常。

测试结束时的.VerifyAll 方法也很好地通过了,这真的很奇怪。这是 Moq 中的错误吗?有解决方法吗?

我也尝试过在此方法上设置回调并在其中执行 Assert.Fail ,但由于这也被吞没,测试框架(VS 2008 内置测试)不会收到通知...

我是使用起订量 2.6.1014.1。 (还没有时间升级到最小​​起订量 3)

I am trying to test a fairly complex class using Moq and am running into a problem.

I am trying to verify that a method does NOT get called, and usually this is simple to do by setting MockBehavior.Strict, but here however the class has its own error reporting mechanism so it swallows the exception being thrown by Moq.

.VerifyAll method at the end of the test also passes fine, which is really weird. Is this a bug in Moq, are there any workarounds?

I've also tried setting up a callback on this method and doing Assert.Fail inside of it, but as this gets swallowed as well, the testing framework (VS 2008 builtin test) doesn't get notified of it...

I am using Moq 2.6.1014.1. (didn't have time to upgrade to moq 3 yet)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳根太软 2024-08-11 23:29:24

好吧,这很尴尬,我已经解决了。

在 3.0 中你可以这样做:

mFMXmlC.Verify(f=>f.Put_Queue_Response(It.IsAny<Uri>(), 
                                       It.IsAny<string>(), 
                                       It.IsAny<string>(), 
                                       It.IsAny<object>()), Times.Never());

仍然不明白为什么 VerifyAll 不起作用,但这似乎符合要求。我会留下这个问题,以防其他人正在寻找类似的东西。

Well, this is embarrassing, I've managed to solve it.

In 3.0 you can do this:

mFMXmlC.Verify(f=>f.Put_Queue_Response(It.IsAny<Uri>(), 
                                       It.IsAny<string>(), 
                                       It.IsAny<string>(), 
                                       It.IsAny<object>()), Times.Never());

Still don't understand why VerifyAll didn't work, but this seems to fit the bill anyway. I'll leave the question up, in case someone else is looking for something similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文