Lesson 32 of 93 · 5 min read · Updated

State, Output and Behaviour Verification in Unit Tests: Which to Prefer

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

  1. .NET 11 SDK
  2. xUnit 2.9.3
  3. The hand-written doubles from the previous lessons: FakeInventory, SpyEmailSender, StubOrderNumberGenerator and DummyAuditLog.

Three Questions, Three Kinds of Verification

Each style of verification answers its own question:

StyleQuestion it answersExample in this test
OutputWhat did the method return?result.OrderNumber == "SIP-9"
StateWhat changed?inventory.AvailableQuantity("KLAVYE-01") == 1
BehaviourWhich 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

using M04.TestDoubles.Tests.Doubles;

namespace M04.TestDoubles.Tests;

/// <summary>
/// "State and behaviour verification answer different questions"
///
/// STATE : what CHANGED? → inventory.AvailableQuantity() == 0
/// OUTPUT : what was RETURNED? → result.OrderNumber == "SIP-1"
/// BEHAVIOUR : which INTERACTION happened? → was the email sent?
///
/// DEFAULT CHOICE: state/output. These are the PROMISE the system makes to the outside world,
/// and they do not break when you refactor the internals.
/// Use behaviour verification only when you cannot observe the effect any other way.
/// </summary>
public class StateVsBehaviourTests
{
[Fact]
public void Place_VerifiedThreeWays()
{
var inventory = new FakeInventory(new Dictionary<string, int> { ["KLAVYE-01"] = 4 });
var spy = new SpyEmailSender();
var sut = new OrderService(
inventory, new StubOrderNumberGenerator("SIP-9"), spy, new DummyAuditLog());

var result = sut.Place("ali@ornek.com", [new OrderLine("KLAVYE-01", 3, 750m)]);

// 1) OUTPUT verification: the most stable, first choice.
Assert.True(result.IsSuccessful);
Assert.Equal("SIP-9", result.OrderNumber);

// 2) STATE verification: also stable.
Assert.Equal(1, inventory.AvailableQuantity("KLAVYE-01"));

// 3) BEHAVIOUR verification: only for an unobservable effect.
Assert.Single(spy.Sent);
}

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.

  1. Reserve all lines in one batch call instead of one call per line.
  2. Check stock through a cached lookup instead of asking the inventory for every line.
  3. Send the email through a background queue that still calls the same IEmailSender once.

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

[Fact]
public void Total_NeedsNoDoublesAtAll()
{
// The best test is one that needs no double at all.
// That is why separating pure functions (testable design) is valuable.
var total = OrderService.Total(
[
new OrderLine("KLAVYE-01", 2, 750m),
new OrderLine("MOUSE-01", 1, 250m),
]);

Assert.Equal(1_750m, total);
}
}

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

  1. Output verification checks what a method returned; state verification checks what changed; behaviour verification checks which interactions happened.
  2. Output and state checks describe the promise the system makes, so they survive refactoring. Make them your default.
  3. Behaviour verification is for outgoing effects that cannot be observed any other way, such as sending an email.
  4. Strict interaction checks on queries or internal steps cause false failures when the code is refactored.
  5. Pure functions such as OrderService.Total need 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.