0
我无法修补请求发帖方法。我读了http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch。但不明白我犯了什么错误。无法修补请求发帖
结构
tests.py
package
__init__.py
module.py
包/ module.py
import requests
import mock
class Class(object):
def send_request(self):
...
response = requests.post(url, data=data, headers=headers)
return response
tests.py
@mock.patch('package.module.requests.post')
def some_test(request_mock):
...
data = {...}
request_mock.return_value = data
# invoke some class which create instance of Class
# and invokes send_request behind the scene
request_mock.assert_called_once_with()
回溯
Traceback (most recent call last):
File "tests.py", line 343, in some_test
request_mock.assert_called_once_with()
File "/home/discort/python/project/env/local/lib/python2.7/site-packages/mock/mock.py", line 941, in assert_called_once_with
raise AssertionError(msg)
AssertionError: Expected 'post' to be called once. Called 0 times.
你确定'package.module.'应该是'@ mock.patch'字符串的一部分吗? – goncalopp
不,但'requests.post'也不能修补 – discort