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

五迷三道 提交于 2019-12-01 15:04:50

问题


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)


回答1:


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.



来源:https://stackoverflow.com/questions/1445745/moq-how-to-verify-method-has-not-been-called-if-the-class-swallows-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!