Bird
0
0

Given this test code snippet, what will be printed?

medium📝 component behavior Q13 of 15
Django - Testing Django Applications
Given this test code snippet, what will be printed?
from unittest.mock import patch

def get_data():
    return external_service_call()

@patch('myapp.services.external_service_call')
def test_get_data(mock_call):
    mock_call.return_value = {'status': 'ok'}
    result = get_data()
    print(result)
AError: external_service_call not defined
BNone
Cexternal_service_call
D{'status': 'ok'}
Step-by-Step Solution
Solution:
  1. Step 1: Understand patch effect on external_service_call

    The patch replaces external_service_call with a mock that returns {'status': 'ok'} only inside the decorated function test_get_data.
  2. Step 2: Analyze get_data call outside patch scope

    get_data() calls external_service_call(), but external_service_call is not defined globally, leading to a NameError.
  3. Final Answer:

    Error: external_service_call not defined -> Option A
  4. Quick Check:

    external_service_call undefined outside patch = D [OK]
Quick Trick: Patch only affects references inside the decorated function [OK]
Common Mistakes:
MISTAKES
  • Assuming patch affects global scope
  • Expecting mock return_value outside patch
  • Ignoring NameError due to missing definition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes