0
0
Microservicessystem_design~10 mins

Unit testing services in Microservices - Interactive Code Practice

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

Complete the code to mock the service dependency in a unit test.

Microservices
mock_service = [1]('UserService')
Drag options to blanks, or click blank then click option'
ASpy
BStub
CFake
DMock
Attempts:
3 left
💡 Hint
Common Mistakes
Using Stub instead of Mock, which is less flexible for verifying interactions.
2fill in blank
medium

Complete the code to assert the service method was called once.

Microservices
mock_service.[1].assert_called_once()
Drag options to blanks, or click blank then click option'
Asome_method
Bprocess
Cexecute
Dfetch_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic names like 'execute' or 'process' that do not match the actual method.
3fill in blank
hard

Fix the error in the test setup to correctly patch the service.

Microservices
with patch('[1].UserService') as mock_service:
Drag options to blanks, or click blank then click option'
Aservice_layer
Bservice
Cservices
DserviceLayer
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect module names that cause patch to fail.
4fill in blank
hard

Fill both blanks to create a test that mocks a service method and sets its return value.

Microservices
mock_service.[1].return_value = [2]
Drag options to blanks, or click blank then click option'
Aget_user
B{'id': 1, 'name': 'Alice'}
Cfetch_data
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Setting return_value to None or wrong method names.
5fill in blank
hard

Fill all three blanks to write a unit test that mocks a service, calls the method, and asserts the result.

Microservices
def test_[1]():
    with patch('services.UserService') as mock_service:
        mock_service.[2].return_value = [3]
        result = mock_service.[2]()
        assert result == [3]
Drag options to blanks, or click blank then click option'
Aget_user
C{'id': 42, 'name': 'Bob'}
Dfetch_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in mock and call, or mismatched return values.