CodePark/ questions / TDD & test design
START PRACTICINGPRACTICE
Backend question · Backend testing › TDD & test design

A bug report says refunds for partially shipped orders are calculated wrongly. How do you fix it the TDD way?

First, reproduce the bug as a failing test.

Write the test at the lowest level that can show the problem. This is often a unit test of the refund calculation. Use the exact order from the report: two items shipped, one not shipped. Run it and watch it fail with the same wrong amount the customer saw. If it passes, you haven't understood the bug yet.

Then:

  • Fix the code until the test passes.
  • Run the whole suite to check nothing else broke.
  • Refactor if the fix made the code messier.

Keep the test as a regression test (a test that stops the bug from coming back). Name it for the behaviour, like "refunds only unshipped items", not for the ticket number.

Also ask why the existing tests missed the bug. Add the nearby cases too:

  • nothing shipped
  • everything shipped
  • an order with a single item
  • First, reproduce the bug with a failing test.
  • Write the test at the lowest level that shows the problem.
  • Fix the code, run the full suite, then refactor.
  • Keep it as a named regression test and add the nearby cases.