Call verification checks that expected interactions occur with the required multiplicity and that prohibited interactions do not occur. It is most useful at system boundaries, where controller tests verify communication with unmanaged dependencies. section 9.2
Apply call verification with these rules:
- Verify the number of calls, rather than merely confirming that a matching call occurred; a message expected once should be checked with an explicit once constraint. section 9.2
- Verify that no other calls occurred when the behavior requires a tightly controlled interaction boundary. section 9.2
- Use as many mocks as the behavior requires; the number of mocks does not determine whether a test covers one unit of behavior. section 9.2
Verifying a message dispatch
After changing a user's email, verify that the message bus sends the expected email-changed message exactly once, then verify that no other calls were made. section 9.2
messageBusMock.Verify(
x => x.SendEmailChangedMessage(user.UserId, "new@gmail.com"),
Times.Once);
messageBusMock.VerifyNoOtherCalls(); #1
Inspect the compact verification pattern combining exact multiplicity with rejection of unexpected calls. section 9.2
