Green tests, nothing checked
You asked the agent to add tests for `Checkout.total`. The rule: orders over 100 get 10 off. The agent reports: “Added 2 tests, all green, `total` fully covered.” The diff is full of `Mock()` objects and `assert_called_once_with`. Coverage, the share of lines the tests run, did go up. Now suppose someone changes the discount to 20 off by mistake. Would either test go red?
Why mocks drift into testing themselves
When the agent runs tests, the only feedback it gets is red or green. A test that can never go red looks exactly like a good test in that feedback. Real collaborators, such as storage or a payment gateway, are awkward to set up. So the agent mocks them, and sometimes the logic itself, until everything is green on the first try. The tests become a proxy signal: they run lines and raise coverage but check nothing. **Later cost:** the suite stays green while `total` charges the wrong amount, and every later change to `total` looks safe.
Three tells and one question
1. **The unit under test is mocked or patched itself**: `patch.object(Checkout, ...)`, Mockito `spy` plus `doReturn` on the service, `jest.spyOn(svc, ...)` plus `mockReturnValue`.
2. **An assertion checks a mock's canned value**: the mock returned what the test configured it to return.
3. **Only call checks** (`assert_called*`, `verify`, `toHaveBeenCalled`) and no check of a result or state.
**Later cost** of all three: the suite stays green while the logic is wrong, so bugs ship and refactors look safe.
**Settle it:** if the unit's logic were wrong, would this test go red?
BAD: what the agent wrote
Test 1 replaces `discount`, a method of the very object under test, so the real rule never runs. It then checks only that calls happened and ignores the result of `total`. Test 2 calls the mock and asserts it returned `[50, 50]`, the value set one line earlier. Change the discount to 20 off and neither test goes red. Both also stay green if `total` adds wrongly or never returns its result.
// Python BAD
def test_total_applies_discount():
repo = Mock()
repo.cart.return_value = [50, 50]
co = Checkout(repo)
co.discount = Mock(return_value=10)
co.total("u1")
repo.cart.assert_called_once_with("u1")
co.discount.assert_called_once()
def test_repo_returns_cart():
repo = Mock()
repo.cart.return_value = [50, 50]
assert repo.cart("u1") == [50, 50]
BETTER: real logic, fake at the edge
Keep the real `Checkout`. Replace only the edge it talks to, storage, with a fake: `FakeRepo` really stores carts. The expected values come from the rule, including the edge case of exactly 100. Break the rule in `total` and at least one test goes red. 20 off gives 100, not 110. A discount at exactly 100 gives 90, not 100. Check calls only when the call *is* the outcome, such as an email being sent. Even then, check the call's arguments.
// Python BETTER
class FakeRepo:
def __init__(self, carts):
self.carts = carts
def cart(self, user):
return self.carts[user]
def test_10_off_over_100():
repo = FakeRepo({"u1": [70, 50]})
assert Checkout(repo).total("u1") == 110
def test_no_discount_at_100():
repo = FakeRepo({"u1": [50, 50]})
assert Checkout(repo).total("u1") == 100
Remember
Some tests cannot go red when the logic is wrong. Watch for three kinds: tests that mock the code they test, tests that assert a mock's own canned value, and tests that only count calls. To fix one, run the real unit, fake only what sits at its edges, and assert the result the spec demands.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.