嘲笑行为和烧瓶

问题描述 投票:0回答:1

我正在尝试使用模拟来模拟HTTP请求调用,因为我不想Behave实际调用它。

所以我在matches.py文件中有此代码方案:

import request

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

并且在我的行为的步骤定义match_steps.py中,我有这个:

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"}

但是似乎这不起作用,因为它仍在请求实际的HTTP请求。

我需要模拟get_match方法以返回{"status": "success"}结果

python flask bdd python-mock python-behave
1个回答
0
投票

好吧,我知道了,您需要将您的初始化放在模拟中,这样:

from matches import get_match


with patch ('match') as mock_match:
      mock_match.retun_value = {"status": "success"}
      get_match()
© www.soinside.com 2019 - 2024. All rights reserved.