9

I was trying to perform subsequent calls verification and I found that moq supports the InSequence() method for this, like:

MockSequence s = new MockSequence();
validator.InSequence(s).Setup(m => m.IsValid(It.IsAny<Frame>())).Returns(true);
encryptor.InSequence(s).Setup(m=>m.Encrypt(It.IsAny<Frame>()));
socket.InSequence(s).Setup(m => m.Send(It.IsAny<Frame>()));
compressor.InSequence(s).Setup(m => m.Compress(It.IsAny<Frame>()));

However, this seems to be working only when I specify mock behavior as "strict", which forbids me calling additional mehods on mocked objects. I'd like, however, to be able to call other methods on those objects, I just want THESE calls to be performed in sequence.

Is there any "supported" way for that (instead of resorting to .Callback() and handmade implementation)? I found an additional library called moq.sequence, however, the precompiled version doesn't work with latest Moq.

Erik Dietrich
  • 6,080
  • 6
  • 26
  • 37

1 Answers1

9

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.

Community
  • 1
  • 1
  • Have you posted all of this to the **Moq's Issues** site too? You had to research the subject, so I assume it was not explained in the documentation -so, they may be not aware of this problem. I can assure you that they look into the issues/suggestions posted there. It takes some time, but they do. I've myself sent a few useful patches (mocking delegates, Count-calls, etc). If you don't want to get involved, I can double check the issue and register it there, but I think that as you already know the details, you would describe it much better:) [btw. sorki, nie chciałem polskim mieszać na SO:)] – quetzalcoatl Dec 09 '12 at 11:49
  • Hi, quetzalcoatl. I didn't know that Moq was actually maintained, there are at least two or three source code repositories for Moq. Is this the current one: https://github.com/Moq/moq4 ? I'll have a few days of holidays before Christmas and before new year, so maybe I'll try to put something together or at least investigate further. – Grzesiek Galezowski Dec 14 '12 at 12:06
  • Yes, indeed, the current one is that on the GitHub. Previously Moq was hosted on code.google, but they recently moved to GitHub. The issue list seen on GitHub are the most recent ones, and new issues should be reported there. The site on code.google still exists and was not deleted/removed/etc, due to the number of reported "old" issues and feature requests. They were not moved to GitHub, IIRC, as there was no way to import them, and noone really wanted to copy-paste them by hand! They are still considered, but I think that issues from GitHub have natural 'priority' over old code.google. – quetzalcoatl Dec 15 '12 at 12:11
  • There already was an issue on this (https://github.com/Moq/moq4/issues/21), so I submitted a comment there along with my own alternative prototype implementation. – Grzesiek Galezowski Dec 18 '12 at 18:15
  • I have created my own implementation of sequential verification for Moq at: https://github.com/grzesiek-galezowski/moq4. The changes need some review, so anybody reading this, please test this implementation and add comments to https://github.com/Moq/moq4/issues/21. The examples on how to use the proposed functionality can be found at: https://github.com/grzesiek-galezowski/moq4/blob/dev/UnitTests/VerifyInSequenceFixture.cs – Grzesiek Galezowski Dec 27 '12 at 22:10