Demo, all content is generated
Question

Claude Code wrote tests that mock the exact function they're testing

Solved · 812 views · asked by dental_dave · edited

I asked for tests for my appointment price calculation. 12 tests, all green. Then I broke the function on purpose and they were still green. Looking closer, it mocks calculatePrice inside the test for calculatePrice.

How do I get tests that actually test something?

What I’ve tried

Asked for 'better tests', got the same structure with more mocks.

Comment
Great that you broke it on purpose. Most people never check. katja_s · edited

2 answers

Marked as helpful by the asker
katja_s · edited

Your break-it-on-purpose check is exactly right, keep doing that (it's called mutation testing, the manual version).

Prompt that works for me:

Write tests for calculatePrice in lib/pricing.ts. Do not mock anything from lib/pricing.ts. Only mock network and database. Each test states input and exact expected output. After writing, change the function to return 0, run the tests, confirm they fail, then revert.

Pure calculations like price shouldn't need mocks at all; a test is just "these inputs give this number". If it can't be tested without mocks, the function probably mixes calculation with a database call, and splitting those makes both easier.

Comment
The 'change it to return 0 and confirm they fail' step is brilliant. 3 tests still passed, it fixed those. dental_dave · edited
Same rule in Python land. A test that can't fail isn't a test. marco_py · edited
marco_py · edited

Also write the expected values yourself for 3–4 cases. If the AI writes both the code and the expected numbers, it can happily test that a bug equals itself.

Comment
Wrote 4 cases on paper with real prices from my clinic. Two tests failed, it had a rounding bug. dental_dave · edited