requests_mock.exceptions.NoMockAddress:在模拟请求库时

时间:2019-11-29 10:31:14

标签: python unit-testing mocking python-unittest python-mock

我正在尝试使用requests_mock库来模拟请求:

@mock.patch('src.commands.random.choice', return_value=15)
@requests_mock.mock()
def test__3rd_party_service__ok(self, m, random_mock, send_message_mock, admins_mock):
    m.get('http://sample.com/RandJSON.aspx?CType=15', text='{"content": "test"}')
    self.commands.fetch_data()
    send_message_mock.assert_called_once_with('test')

原始代码非常简单(src/commands.py):

def fetch_data(self):
    category = random.choice(['11', '13'])
    response = requests.get(f'http://sample.com/RandJSON.aspx?CType={category}').text
    text = json.loads(response, strict=False)
    self.send_message(text['content'])

测试失败,并出现以下错误:

requests_mock.exceptions.NoMockAddress: No mock address: GET http://sample.com/RandJSON.aspx?CType=15

我仍然无法理解问题所在。

感谢帮助!

0 个答案:

没有答案