我有一个名为w.py
的文件,在__name__ == "__main__"
下,我想修补MyAPI
,运行python w.py
显示修补根本不起作用。patch('common.apis.MyAPI', mock_api)
是否正确使用?
from common.apis import MyAPI
class Worker:
def __init__(self):
companies = MyAPI.all()
raise Exception(companies)
if __name__ == "__main__":
from mock import patch, Mock
mock_api = Mock()
mock_api.all.return_value = {'dummy': 'dummy'}
with patch('common.apis.MyAPI', mock_api):
Worker()
1条答案
按热度按时间f2uvfpb91#
IIUC你想给
MyAPI.all
打补丁,让它返回{'dummy': 'dummy'}
,你可以这样做: