Challenge - 5 Problems
Mocking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Django test using mock?
Consider this Django test case that mocks an external API call. What will be the printed output when the test runs?
Django
from unittest.mock import patch from django.test import TestCase class ExternalService: def fetch_data(self): # Imagine this calls an external API return 'real data' class MyTest(TestCase): @patch.object(ExternalService, 'fetch_data') def test_mocked_fetch(self, mock_fetch): mock_fetch.return_value = 'mocked data' service = ExternalService() result = service.fetch_data() print(result) self.assertEqual(result, 'mocked data')
Attempts:
2 left
💡 Hint
Look at what the mock replaces and what return_value is set to.
✗ Incorrect
The patch decorator replaces the fetch_data method with a mock that returns 'mocked data'. So calling fetch_data returns 'mocked data'.
📝 Syntax
intermediate1:30remaining
Which option correctly mocks a Django external service method?
You want to mock the method 'get_user_info' of the class 'UserService' located in 'myapp.services'. Which of these patch decorators is correct?
Attempts:
2 left
💡 Hint
The patch path must be the full import path to the method.
✗ Incorrect
The patch decorator requires the full import path to the method to mock it correctly. 'myapp.services.UserService.get_user_info' is the correct full path.
🔧 Debug
advanced2:30remaining
Why does this mock not replace the external call in Django test?
Given this test code, why does the external API call still run instead of being mocked?
Django
from unittest.mock import patch from django.test import TestCase class ExternalAPI: def call(self): return 'real response' class MyTest(TestCase): @patch('external_api.ExternalAPI.call') def test_api_call(self, mock_call): mock_call.return_value = 'mocked response' api = ExternalAPI() response = api.call() print(response) self.assertEqual(response, 'mocked response')
Attempts:
2 left
💡 Hint
Check where ExternalAPI is imported and how patch paths work.
✗ Incorrect
Patch must target the exact location where the object is used. If ExternalAPI is imported differently in the test module, patching 'external_api.ExternalAPI.call' won't replace the method.
❓ state_output
advanced2:00remaining
What is the value of 'call_count' after this Django test runs?
In this test, how many times was the mocked method called?
Django
from unittest.mock import patch from django.test import TestCase class Service: def fetch(self): return 'data' class MyTest(TestCase): @patch.object(Service, 'fetch') def test_calls(self, mock_fetch): mock_fetch.return_value = 'mocked' service = Service() service.fetch() service.fetch() count = mock_fetch.call_count print(count) self.assertEqual(count, 2)
Attempts:
2 left
💡 Hint
call_count tracks how many times the mock was called.
✗ Incorrect
The mock replaces fetch, so each call increments call_count. Two calls mean call_count is 2.
🧠 Conceptual
expert3:00remaining
Which statement best describes mocking external services in Django tests?
Select the most accurate statement about mocking external services in Django tests.
Attempts:
2 left
💡 Hint
Think about the scope and purpose of mocking in tests.
✗ Incorrect
Mocking temporarily replaces calls during tests to avoid real external calls and control test conditions. It does not change code permanently or require modifying external services.