Skip to main content

Practice · 2 of 2

Fake the API

CHALLENGE
Difficulty: intermediate+25 XP

Implement fetch_json(url, opener=None): opener defaults to a real-ish callable; the function calls opener(url) expecting an object with .read() returning bytes of JSON, and returns the parsed dict. Write TestFetch using unittest.mock to pass a fake opener returning b'{"ok": true}' and assert the parsed result AND that the fake was called exactly once with the URL. Assign to SUITE_TEST.

Back to lesson: Practice: Mock Drills