Bird
0
0

Identify the error in this test code snippet:

medium📝 Debug Q6 of 15
Django - Testing Django Applications
Identify the error in this test code snippet:
from unittest.mock import patch

@patch('external.api.get_data')
def test_api():
    response = get_data()
    assert response == 'mocked'
APatch decorator is missing parentheses
Bget_data is not imported or defined in test scope
CMock return value is not set
DPatch target string is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check function usage inside test

    The test calls get_data() but get_data is not imported or defined in the test scope.
  2. Step 2: Understand patch does not import function

    Patch replaces the function where it is used, but the test must have get_data accessible to call it.
  3. Final Answer:

    get_data is not imported or defined in test scope -> Option B
  4. Quick Check:

    Function must be accessible to call after patch [OK]
Quick Trick: Import or define function before calling in test [OK]
Common Mistakes:
MISTAKES
  • Assuming patch imports function automatically
  • Ignoring missing return_value setting
  • Confusing patch target string format

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes