Bird
0
0

Which approach correctly mocks the external API call and returns a custom JSON response {"success": true} during the test?

hard📝 Application Q15 of 15
Flask - Testing Flask Applications
You want to test a Flask route that calls an external API and returns JSON data. Which approach correctly mocks the external API call and returns a custom JSON response {"success": true} during the test?
AUse patch on the Flask route function and return {"success": true} directly
BUse patch on the external call, set return_value.json.return_value = {"success": true}
CCall the real external API and check if it returns {"success": true}
DMock the Flask app config to include {"success": true}
Step-by-Step Solution
Solution:
  1. Step 1: Identify what to mock

    The external API call (e.g., requests.get) should be mocked to avoid real network calls.
  2. Step 2: Set mock return JSON data

    Set the mock's return_value.json.return_value to the desired JSON dict to simulate API response.
  3. Final Answer:

    Use patch on the external call, set return_value.json.return_value = {"success": true} -> Option B
  4. Quick Check:

    Mock external call's json() method = Use patch on the external call, set return_value.json.return_value = {"success": true} [OK]
Quick Trick: Mock return_value.json.return_value for JSON responses [OK]
Common Mistakes:
MISTAKES
  • Mocking Flask route instead of external call
  • Calling real API in tests
  • Changing app config instead of mocking

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes