简体   繁体   English

moq-如果类吞下异常,如何验证方法未被调用

[英]moq - how to verify method has not been called if the class swallows exceptions

I am trying to test a fairly complex class using Moq and am running into a problem. 我正在尝试使用Moq测试相当复杂的类,并且遇到了问题。

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. 我试图验证没有调用方法,通常这很容易通过设置MockBehavior.Strict来完成,但是在这里,该类具有自己的错误报告机制,因此可以吞下Moq抛出的异常。

.VerifyAll method at the end of the test also passes fine, which is really weird. 测试结束时的.VerifyAll方法也可以通过,这确实很奇怪。 Is this a bug in Moq, are there any workarounds? 这是Moq中的错误,是否有任何解决方法?

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... 我还尝试过在此方法上设置回调并在其中执行Assert.Fail,但是由于吞噬了它,因此测试框架(VS 2008内置测试)也没有得到通知...

I am using Moq 2.6.1014.1. 我正在使用Moq 2.6.1014.1。 (didn't have time to upgrade to moq 3 yet) (还没有时间升级到最低订购量3)

Well, this is embarrassing, I've managed to solve it. 好吧,这很尴尬,我已经设法解决了。

In 3.0 you can do this: 在3.0中,您可以执行以下操作:

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. 仍然不明白为什么VerifyAll不起作用,但是无论如何这似乎符合要求。 I'll leave the question up, in case someone else is looking for something similar. 如果有人在寻找类似的东西,我将不提问题。

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

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