Moq expectations by the same method twice in a row

I am trying to set up exceptions for a method that is called twice in a row with different parameters. Like this:

var adapter = new Mock<IKeyAdapter>(); adapter.Setup(x => x.ImportKey(It.IsAny<Guid>(), key, It.IsAny<string>(), publicTicket)).Returns(Guid.NewGuid()); adapter.Setup(x => x.ImportKey(It.IsAny<Guid>(), key, It.IsAny<string>(), privateTicket)).Returns(Guid.Empty); 

I am not the first to pass, but the second is to fail. Currently, it seems that the second setting overwrites the first.

Is this possible with Moq?

+8
c # testing moq
source share
1 answer

You are right that the second wait overwrites the first. This seems to be the current limitation. Some workarounds were designed as follows:

  • Moq sequences that can be obtained here on github.
  • Overloading IExpect.Returns for the expression, not the value described here , and was developed on here and here .
+6
source share

All Articles