AI-assisted coding and maintenance · Module 1: AI-assisted coding and maintenance in practice
Test the bug, not the implementation
Learning outcome: Create a failing behavioural case before fixing a defect.
A useful regression test reproduces the behaviour that matters to a user. It should fail for the known defect and pass when the defect is fixed. A test that merely repeats the implementation's internal formula may preserve the same mistake.
Use independently worked expected values. Keep the test small enough that a failure is understandable. Where a defect spans storage or a network boundary, test the boundary rather than replacing every interesting component with a mock. After fixing the issue, run the focused case and the relevant surrounding checks. Broader tests should answer a concrete risk, not substitute for understanding the failure.
Worked example
A fictional app restores a saved stage value of 9 even though only stages 0, 1 and 2 exist. The page goes blank. A useful test supplies the invalid saved value and expects rejection or an explicit recovery state. Testing that the renderer accepts whatever number it receives would encode the defect.
Try it yourself
Define tests for missing stage, stages 0 and 2, and invalid values -1, 3 and a string. Describe the intended recovery without writing the implementation.
You can do this exercise on paper. If using an assistant, keep to the fictional material and an approved tool.
Compare your approach
The default for a missing optional stage can be 0 if that is the contract. Out-of-range and wrong-type values need rejection or a documented recovery. The test should not silently turn every corrupt value into valid progress.
Check your understanding
Which test best protects against the defect?
- The component uses the same variable name as before
- The page has at least one div element
- An invalid saved stage produces the specified recovery behaviour
Show the answer and explanation
Answer: An invalid saved stage produces the specified recovery behaviour. The behavioural test supplies an invalid stage and checks the specified recovery. Variable names and element counts do not establish that a user can recover from corrupt state.
Workbook
Download the course workbook to record your answer and compare it after the knowledge check.