Bird
0
0

Identify the issue in this Django test patching example:

medium📝 Debug Q7 of 15
Django - Testing Django Applications
Identify the issue in this Django test patching example:
from unittest.mock import patch

@patch('myapp.api.requests.get')
def test_api_call(mock_get):
    mock_get.return_value.status_code = 200
    mock_get.return_value.json.return_value = {'result': 'success'}
    # test logic here
AThe mock_get parameter should be named 'mock_requests_get'
BThe mock object cannot set 'json.return_value'
CThe patch decorator must be applied inside the test function
DThe patch target should be where 'requests.get' is imported in the code under test
Step-by-Step Solution
Solution:
  1. Step 1: Understand patch target

    Patch must target the name used in the module under test, not the original import path.
  2. Step 2: Check patch usage

    Here, patching 'myapp.api.requests.get' is correct only if 'requests.get' is imported there.
  3. Final Answer:

    The patch target should be where 'requests.get' is imported in the code under test -> Option D
  4. Quick Check:

    Patch where used, not where defined [OK]
Quick Trick: Patch where the function is used, not defined [OK]
Common Mistakes:
MISTAKES
  • Patching the original module instead of usage location
  • Misnaming mock parameters
  • Incorrectly setting mock return values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes