When a bug report arrives, I usually want to reach for the code straight away. If the cause looks obvious, it's tempting to make the change and check that the original example now works.

I try to put a test in between those two things.

That test makes me explain what I think has gone wrong. It also gives me a way to check the fix after I've moved on to something else, when the details of the report are no longer fresh in my head.

Start with what the person was doing

Consider a report that editing a customer has cleared their business name.

There are several possible explanations. The browser might have sent an empty field. The application might have interpreted a missing field as an instruction to clear it. A validation rule might have excluded a value that the save operation still expected.

Those possibilities suggest places to investigate. The reported behaviour gives us the first thing to test: a customer has a business name, an authorised person changes another detail, and the business name should survive.

I'd start with a feature test that exercises the relevant update route or component action and then checks the saved record. It should describe the operation closely enough that somebody reading it can connect it to the bug report.

Make sure the failure explains the bug

A red test needs a read. If it failed because the user wasn't authenticated, it hasn't reproduced our data problem. If it failed because the fixture omitted a required relationship, we may only have proved that the setup is incomplete.

I want to see the expected business name compared with the value that was actually saved. That gives me some confidence that the test has reached the behaviour I intended to investigate.

The setup should be small enough to follow, but it still needs the details that make the bug possible. Factories help with the ordinary parts. The important values belong in the test where I can see them.

Assert the result somebody depends on

For that customer update, a successful response would be useful to check. It wouldn't tell us whether the business name survived.

I'd reload the record and inspect the relevant values. If the operation is supposed to send a notification or dispatch background work, those effects may need assertions too. Which ones matter depends on the report and the intended behaviour.

I try to avoid locking the test to every method call inside the implementation. Otherwise, extracting a small class can break the test even though the application still behaves correctly.

There are good reasons to test a calculation or transformation directly. I'd reach for a unit test when that smaller piece has rules worth explaining independently. For a problem involving the request, validation and persistence together, I want at least one test that keeps that route through the application intact.

Check the decisions either side of the fix

Once the original test passes, I look at the neighbouring cases.

If omitting the business name preserves it, can someone still deliberately clear it? If an authorised user can make the change, what happens when somebody without permission tries? If one customer is updated, are other customers left alone?

Those checks follow from the behaviour we've changed. I don't need a dozen variations of the same assertion, but I do want to catch a fix that solves the report by preventing a legitimate operation elsewhere.

I like finishing a bug fix with a test name I could recognise in six months. Something that tells me what a person was trying to do, what the application must preserve, and why this particular bit of behaviour is now covered.