Verifying in Sequence
Clash Royale CLAN TAG#URR8PPP
Verifying in Sequence
I am trying to verify a set of method calls in sequence.
Here is a sample of what I would like to do. This test should fail, but it actually passes:
public interface IMyInterface
void Method(int i);
public class MyClass : IMyInterface
public void Method(int i)
[TestMethod]
public void MyTestMethod()
var mock = new Mock<IMyInterface>();
var listOfThings = new List<int> 5, 4, 3, 2, 1 ;
MethodUnderTest(mock.Object, listOfThings);
mock.Verify(m => m.Method(1));
mock.Verify(m => m.Method(2));
mock.Verify(m => m.Method(3));
mock.Verify(m => m.Method(4));
mock.Verify(m => m.Method(5));
public void MethodUnderTest(IMyInterface myInterface, List<int> things)
foreach (var i in things)
myInterface.Method(i);
This should fail, as the Verify
calls expect a different order of parameters.
Verify
I have tried MockSequence
like this:
MockSequence
[TestMethod]
public void MyTestMethod()
var mock = new Mock<IMyInterface>();
var listOfThings = new List<int> 5, 4, 3, 2, 1 ;
var s = new MockSequence();
mock.InSequence(s).Setup(m => m.Method(1));
mock.InSequence(s).Setup(m => m.Method(2));
mock.InSequence(s).Setup(m => m.Method(3));
mock.InSequence(s).Setup(m => m.Method(4));
mock.InSequence(s).Setup(m => m.Method(5));
MethodUnderTest(mock.Object, listOfThings);
mock.Verify(m => m.Method(1));
mock.Verify(m => m.Method(2));
mock.Verify(m => m.Method(3));
mock.Verify(m => m.Method(4));
mock.Verify(m => m.Method(5));
But I guess I'm doing this wrong.
Using MockBehaviour.Strict
doesn't seem to work either:
MockBehaviour.Strict
[TestMethod]
public void MyTestMethod()
var mock = new Mock<IMyInterface>(MockBehavior.Strict);
var listOfThings = new List<int> 5, 4, 3, 2, 1 ;
mock.Setup(m => m.Method(1));
mock.Setup(m => m.Method(2));
mock.Setup(m => m.Method(3));
mock.Setup(m => m.Method(4));
mock.Setup(m => m.Method(5));
MethodUnderTest(mock.Object, listOfThings);
mock.Verify(m => m.Method(1));
mock.Verify(m => m.Method(2));
mock.Verify(m => m.Method(3));
mock.Verify(m => m.Method(4));
mock.Verify(m => m.Method(5));
I can't use a Setup
to configure the parameters passed into the mock call, as these values do not come from a mockable source.
Setup
MethodUnderTest
myInterface.Method
things
@Spotted Correct.
– rhughes
Aug 6 at 6:36
As far as I understand (I don't use mocks) your test will fail anyway because of
listOfThings
declared as 5, 4, 3, 2, 1
and expecting beeing used as 1, 2, 3, 4, 5
.– Spotted
Aug 6 at 6:38
listOfThings
5, 4, 3, 2, 1
1, 2, 3, 4, 5
This test is supposed to fail, but it passes.
– rhughes
Aug 6 at 6:40
Ok. 1) I'm not sure I understand the purpose of writing a test that fails. 2) I don't see any value to test
MethodUnderTest
as there is no relevant logic that one may be interested to verify. 3) Verifying method calls with mocks (and even more a calls sequence) is considered a bad practice. If you are willing, I propose that you edit your code with the real meaning and that I help you to either refactor your test so that it provides a value or to conclude that your test is useless and remove it.– Spotted
Aug 6 at 6:50
MethodUnderTest
1 Answer
1
Creating your mock with MockBehavior.Strict
var mock = new Mock<IMyInterface>(MockBehavior.Strict);
will allow you to verify the calls are in sequence.
The complete method would look like
[TestMethod]
public void MyTestMethod()
var mock = new Mock<IMyInterface>(MockBehavior.Strict);
//will fail
var listOfThings = new List<int> 5, 4, 3, 2, 1 ;
//will pass
var listOfOtherThings = new List<int> 1, 2, 3, 4, 5 ;
var s = new MockSequence();
mock.InSequence(s).Setup(m => m.Method(1));
mock.InSequence(s).Setup(m => m.Method(2));
mock.InSequence(s).Setup(m => m.Method(3));
mock.InSequence(s).Setup(m => m.Method(4));
mock.InSequence(s).Setup(m => m.Method(5));
MethodUnderTest(mock.Object, listOfThings);
mock.Verify(m => m.Method(1));
mock.Verify(m => m.Method(2));
mock.Verify(m => m.Method(3));
mock.Verify(m => m.Method(4));
mock.Verify(m => m.Method(5));
This doesn't seem to work. I have updated my question.
– rhughes
Aug 6 at 6:37
you have to use the InSequence in combination with MockBehaviour.Strict. I've updated the answer with a full example
– Mel Gerats
Aug 6 at 7:03
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What do you really want to test ? That
MethodUnderTest
callsmyInterface.Method
withthings
in the given order ?– Spotted
Aug 6 at 6:35