Skip to main content

Practice ยท 2 of 2

Fake the API

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.

Difficulty: intermediate

Back to lesson: Practice: Mock Drills