Mocking in Behave and Flask

Question:

I am trying to mock an HTTP request call using mock because I don’t want to Behave to call it actually.

So I have this code scenario in matches.py file:

import request

def get_match():
   response = request.get("https://example.com")
   return response

And in my step definition match_steps.py for behave I have this:

def logoport_matches_response(context):
   mock_response = context.text # this is the payload will come from feature file
   with patch ('match') as mock_match:
      mock_match.get_match.return_value = {"status": "success"}

But it seems this is not working because it still requesting an actual HTTP request.

I need to mock the get_match method to return {"status": "success"} result

Asked By: Jann Anthony Briza

||

Answers:

Alright, I figure it out, you need to put your initialization inside the mock so:

from mock import patch
from matches import get_match


with patch ('match') as mock_match:
      mock_match.return_value = {"status": "success"}
      get_match()
Answered By: Jann Anthony Briza