0
0
Djangoframework~20 mins

Mocking external services in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mocking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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')
ANone
Breal data
Cmocked data
DRaises AttributeError
Attempts:
2 left
💡 Hint
Look at what the mock replaces and what return_value is set to.
📝 Syntax
intermediate
1: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?
A@patch('myapp.services.UserService.get_user_info')
B@patch('UserService.get_user_info')
C@patch('myapp.UserService.get_user_info')
D@patch('services.UserService.get_user_info')
Attempts:
2 left
💡 Hint
The patch path must be the full import path to the method.
🔧 Debug
advanced
2: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')
AThe mock_call.return_value is set after the call, so it has no effect.
BThe patch path is incorrect; it should match where ExternalAPI is imported in the test module.
CThe ExternalAPI class is not patched, only the method, so the call runs real code.
DThe test method is missing a call to mock_call.start() to activate the patch.
Attempts:
2 left
💡 Hint
Check where ExternalAPI is imported and how patch paths work.
state_output
advanced
2: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)
A2
B1
C0
DRaises AttributeError
Attempts:
2 left
💡 Hint
call_count tracks how many times the mock was called.
🧠 Conceptual
expert
3:00remaining
Which statement best describes mocking external services in Django tests?
Select the most accurate statement about mocking external services in Django tests.
AMocking external services is unnecessary if the service is reliable and always available during tests.
BMocking permanently changes the external service code so all future calls in the app use the mocked version.
CMocking requires modifying the external service code to add special test hooks.
DMocking replaces the external service call only within the test scope, preventing real network requests and allowing controlled responses.
Attempts:
2 left
💡 Hint
Think about the scope and purpose of mocking in tests.