Search the courses, subjects, tracks, games and features, or jump straight to a page.
5 cards from this section. Quick, good the night before a test.
Why is mocking useful in unit testing?
Think about components that are slow, unpredictable, or external.
How can you simulate a method returning a specific value?
The attribute's name says exactly what it controls.
What does mock_api.fetch_temperature.return_value = 30 do?
It controls the output of the mock, not a check on it.
Which assertion checks that a mock was called exactly once with specific arguments?
Its name lists everything it checks.
Write notify_if_hot(api_client, sender, city) that sends an alert with sender.send(message) only when the temperature is 35 or higher. Test both cases with mocks: on a hot day send must be called once with the exact message, and on a mild day it must not be called at all.
A fresh pair of mocks for each case keeps the call records separate.
Write fetch_with_retry(api_client, city, attempts=3) that retries when fetch_temperature raises ConnectionError. Use a mock that fails twice and then returns 30 to show that it succeeds on the third call, and a mock that always fails to show that the error comes through after three attempts.
An exception instance in a `side_effect` list is raised; any other item is returned.
6 more questions in this section. Enroll free to practise them all.
Enroll free