BETTER: real logic, fake at the edge
builds on: Test doubles: stubs, fakes, mocks and spies, What makes a test able to fail
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