Behavior assertions protect the contract users and callers depend on. Implementation assertions can fail on harmless refactoring.
Before you start
You should understand inputs, expected outputs and the boundaries of the component under test. State the risk a test should detect before choosing a tool or mock. Distinguish controlled dependencies from real integrations so the result does not imply more coverage than the test actually provides.
The practical goal is to reason through this situation: Assert the returned error and absent write rather than the exact private helper call sequence. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Define caller-visible results
Invalid input should return a controlled error and perform no write.
Step 2: Choose contract assertions
Inspect outcomes and relevant side effects instead of private helper order.
Step 3: Include valid behavior
Rejection tests alone do not prove permitted inputs still work.
Worked scenario
Assert the returned error and absent write rather than the exact private helper call sequence.
A refactor replaces two validation helpers with one equivalent function. A test checking private helper calls fails despite correct behavior, while a test checking rejected input and zero writes continues protecting the contract. Required external interactions can still be valid assertions when they are part of the actual boundary.
Common mistake
Mocking every internal method can make tests mirror the implementation.
Verify the behavior
Refactor internals and confirm contract tests survive while a deliberate behavior defect fails.
Interview exercise
Test a validation change.
Answer and reasoning
Provide valid and invalid inputs and verify outcomes and side effects, including boundary values.
Continue learning
Compare the scenario with the Software Testing interview questions and test your understanding with the Software Testing MCQs. For terminology and implementation details, consult the reference material.