Bird
0
0

In a Flask test, you want to mock two different external services called sequentially using requests.get. How can you configure the mock to return different responses for each call?

hard📝 Application Q9 of 15
Flask - Testing Flask Applications
In a Flask test, you want to mock two different external services called sequentially using requests.get. How can you configure the mock to return different responses for each call?
ASet mock_get.return_value to a single response and change it manually between calls
BCall patch twice on 'requests.get' with different return_value each time
CUse two separate patch decorators for the same function
DSet mock_get.side_effect to a list of mock responses with different return_value attributes
Step-by-Step Solution
Solution:
  1. Step 1: Understand side_effect for sequential calls

    side_effect can be set to a list of values or mocks to return different results on each call.
  2. Step 2: Apply side_effect with multiple mock responses

    Creating a list of mock responses with different return_value attributes allows each call to requests.get to return a different mocked response.
  3. Final Answer:

    Set mock_get.side_effect to a list of mock responses with different return_value attributes -> Option D
  4. Quick Check:

    Use side_effect list for multiple call responses [OK]
Quick Trick: Use side_effect list to mock multiple sequential calls [OK]
Common Mistakes:
MISTAKES
  • Trying multiple patch decorators on same function
  • Changing return_value manually between calls
  • Calling patch multiple times incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes