Can I use moq's InSequence() with MockBehavior.Loose?

偶尔善良 提交于 2019-12-05 02:31:26
Grzesiek Galezowski

Ok, I investigated the case myself by digging into Moq's source code in the SVN Browser (just for the record - the moq version in question is Moq.4.0.10827.Final).

My investigation led me to: http://code.google.com/p/moq/source/browse/trunk/Source/MockSequence.cs?spec=svn751&r=712

By looking at the InSequence() method, I can see now that the whole implementation is based on the When() method.

So, in reality, the following code:

validator.InSequence(s).Setup(m => m.IsValid(It.IsAny<Frame>())).Returns(true);

ends up as something like:

validator.When(/* call is made in sequence */).Setup(m => m.IsValid(It.IsAny<Frame>())).Returns(true);

In other words, this is just setting up a conditional behavior - when the method is invoked in sequence, the specified Setup() comes into play. Otherwise, default implementation is executed. And because for a strict mock, the default implementation is to throw exception (the call is treated as unspecified), the whole solution works.

Hence, it seems getting current solution to work with loose mocks would be quite cumbersome. I'll just stick to homemade solutions based on Callback() (which can be really nicely wrapped, by the way) - it takes away the ability to use callback for other means, however, I wasn't using it anyway.

I am posting this answer in hope it is useful.

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