State Verification: Testing Outcomes
You'll learn how to verify your tests by checking what changed, not just what was called.
What is State Verification?
State verification means examining the final condition of your system after running code, rather than checking which methods were invoked along the way. You look at the outcome—the actual data, object properties, or visible results—to confirm your code worked correctly.
Think of baking a cake: you could verify success by checking the final cake (state verification)—is it fluffy, golden, the right height?—rather than confirming the mixer ran for exactly 3 minutes (interaction verification).
How It Works
After executing the code under test (the "Act" phase), you inspect:
- Object properties: Did a user's balance decrease by the right amount?
- Return values: Does the function return the expected result?
- System outputs: Was the correct file created? Did the database contain the new record?
- Observable behavior: Did the UI element become visible?
// Example: State verification
function withdraw(account, amount) {
account.balance -= amount;
return account.balance;
}
// Test using state verification
const account = { balance: 100 };
const newBalance = withdraw(account, 30);
// Check the OUTCOME (state):
assert(account.balance === 70); // Object state changed
assert(newBalance === 70); // Return value correct
State vs Interaction
State verification focuses on what happened, while interaction-based testing (using mocks) focuses on how it happened. State verification tends to be more resilient to refactoring because you don't care about internal implementation details—only that the end result is correct.
Key Takeaway:
State verification tests what your code produces or changes, making tests focused on outcomes rather than implementation details—check the cake, not the mixing time.