Lesson 32 of 93 · 5 min read · Updated
State, Output and Behaviour Verification in Unit Tests: Which to Prefer
On this page
There are three ways to check that code did the right thing. You can check what it returned, what state it changed, or which interactions it had with its collaborators. Each answers a different question, and they do not cost the same when the code changes later. This lesson puts all three into one xUnit test for the OrderService from the earlier lessons, then ranks them by how well they survive refactoring.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
- The hand-written doubles from the previous lessons:
FakeInventory,SpyEmailSender,StubOrderNumberGeneratorandDummyAuditLog.
Three Questions, Three Kinds of Verification
Each style of verification answers its own question:
| Style | Question it answers | Example in this test |
|---|---|---|
| Output | What did the method return? | result.OrderNumber == "SIP-9" |
| State | What changed? | inventory.AvailableQuantity("KLAVYE-01") == 1 |
| Behaviour | Which interaction happened? | One confirmation email was sent |
Output and state verification look at results. Behaviour verification looks at how the code talked to its collaborators. That difference decides how fragile the test is.
One Test, Three Assertions
The test below starts with four items in stock, places an order for three, and then checks the result in all three ways.
M04.TestDoubles.Tests/StateVsBehaviourTests.cs
The double for each collaborator matches the kind of check. The fake inventory makes the state check possible, because it really decrements stock: four minus three leaves one. The stub order number generator makes the output check possible, because the number is known in advance. The spy makes the behaviour check possible, because the email sender has no return value.
Which Assertion Survives Refactoring?
Imagine three changes a developer might make to Place later, none of which change what the customer sees.
- Reserve all lines in one batch call instead of one call per line.
- Check stock through a cached lookup instead of asking the inventory for every line.
- Send the email through a background queue that still calls the same
IEmailSenderonce.
The output assertions do not notice any of these changes. The method still returns a successful result with the same number. The state assertion does not notice them either: one item is left in stock, however it got there.
The behaviour assertion is the most sensitive. It survives these three changes because it only checks that one email was sent, which is the business rule. But behaviour checks are easy to make stricter than they should be. Had the test also verified that AvailableQuantity was called once per line, change number 2 would break it while the system still works correctly. That is a false failure, and a team that sees many of them learns to ignore red tests.
So the ranking is: output first, state second, behaviour only for effects you cannot see any other way. The email is such an effect. Stock levels and return values are not.
Common mistake: using behaviour verification out of habit. Mocking libraries make it easy to verify every call, so tests end up asserting the exact sequence of internal steps. Prefer the result. Reach for an interaction check only at an outgoing boundary such as email, messaging or payment.
The Best Double Is No Double
The same file has one more test. It checks the order total, and it uses no double at all.
M04.TestDoubles.Tests/StateVsBehaviourTests.cs
Total is a static, pure function. It takes order lines and returns a number. It reads no stock, sends no email and writes no log. A pure function only needs output verification, which is the most stable kind.
This says something about design, not only about testing. Logic that does not need collaborators should not be mixed with logic that does. When you separate the pure calculation from the code that talks to the outside world, more of your logic becomes testable with plain inputs and outputs, and fewer tests need doubles at all.
Summary
- Output verification checks what a method returned; state verification checks what changed; behaviour verification checks which interactions happened.
- Output and state checks describe the promise the system makes, so they survive refactoring. Make them your default.
- Behaviour verification is for outgoing effects that cannot be observed any other way, such as sending an email.
- Strict interaction checks on queries or internal steps cause false failures when the code is refactored.
- Pure functions such as
OrderService.Totalneed no doubles. Separating pure logic from I/O makes code easier to test. The next lesson brings these rules together into a guide for picking the lightest double.