Doubling at the Boundary

Replace at the edge, not in the middle. Double the HTTP call, not the three functions between your view and it. Doubling deep inside means your test no longer exercises the code between, which is where the bugs are.

The practical version: have one function that makes the request, and double that. This is also why the APIs & Datasets course had you extract the request into its own function.

◈ Graded Challenge

Write make_fake(responses) returning a fake HTTP client for tests. The returned function takes a url and returns the matching entry from the responses dictionary, or the dictionary {"status": 404} when the url is not present. It must also record every url it was called with, in order, on an attribute or property named calls, so a test can inspect what happened.

Name it exactly: make_fake(responses)

2 visible + 4 hidden
loading editor…

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back