liveBook · Manning

Call verification

As explained in Unit Testing Principles, Practices, and Patterns

A testing technique that checks whether expected interactions occur with the required multiplicity and whether prohibited interactions do not occur.

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

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Defines interaction checks, exact call counts, unexpected-call rejection, and the rationale for using multiple mocks.
Examples
Uses an email-change message sent through a message bus and verifies its arguments, count, and surrounding calls.
What it assumes
Assumes familiarity with mocks, integration tests, controllers, and the distinction between units of code and behavior.
What you can do after
Diagnose incorrect orchestration by asserting required calls, exact multiplicity, and absence of unintended interactions.
How it connects
Connects mocks with integration tests, controllers, unmanaged dependencies, and units of behavior.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Unit Testing Principles, Practices, and Patterns. The section links point to the parts of the book it draws on.