0
0
Djangoframework~10 mins

Mocking external services in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the mock library for testing.

Django
from unittest import [1]
Drag options to blanks, or click blank then click option'
Apatch
Bmock
CMagicMock
DTestCase
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'mock' instead of 'patch' when the task asks for the decorator.
Trying to import from 'mock' directly instead of 'unittest.mock'.
2fill in blank
medium

Complete the code to patch the external API call in the test function.

Django
@mock.[1]('myapp.services.external_api_call')
def test_api_call(mock_api):
    pass
Drag options to blanks, or click blank then click option'
AMagicMock
Bside_effect
Ccall
Dpatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'MagicMock' as a decorator instead of 'patch'.
Forgetting to specify the full path to the function to patch.
3fill in blank
hard

Fix the error in the mock setup to return a specific value from the external service.

Django
mock_api.[1] = {'status': 'ok'}
Drag options to blanks, or click blank then click option'
Aside_effect
Breturn_value
Cmock_return
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'side_effect' when only a return value is needed.
Trying to assign to 'return' which is a reserved word.
4fill in blank
hard

Fill both blanks to assert the mock was called exactly once with the expected argument.

Django
mock_api.[1]([2])
Drag options to blanks, or click blank then click option'
Amock_calls
B'test_param'
Cassert_called_once_with
Dcall
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mock_calls' which is a list, not an assertion method.
Passing the wrong argument or forgetting quotes around string arguments.
5fill in blank
hard

Fill all three blanks to create a mock context manager that returns a mock response with status code 200.

Django
with mock.[1]('myapp.services.get_response') as [2]:
    [3].return_value.status_code = 200
Drag options to blanks, or click blank then click option'
Apatch
Bmock_response
Cmock_obj
Dmock_api
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong mock variable name inconsistently.
Not setting the return_value on the mock object.