gpt4 book ai didi

python - 要修补哪个模块

转载 作者:行者123 更新时间:2023-11-28 21:15:44 25 4
gpt4 key购买 nike

我有以下目录

/root
/app
/api
my_api.py
/service
my_service.py
/tests
test_api.py

我的API.py

import app
def run_service():
app.service.my_service.service_function()

测试接口(interface).py

@patch('app.service.my_service.service_function')
test_run_service(self,mock_service):
mock_service.return_value = 'Mock'
response = self.client.get(url_for('api.run_service')
self.assertTrue(response == expected_responce)

以上作品。我想不通的是我需要修补哪个模块,以防我想像这样在 my_apy.py 中导入 service_function:

from app.service.my_service import service_function

如果我像上面那样进行导入,模拟将停止工作。

最佳答案

您需要修补 app.api.my_api.service_function,因为这是已绑定(bind)到导入对象的全局名称:

@patch('app.api.my_api.service_function')
test_run_service(self, mock_service):
mock_service.return_value = 'Mock'
response = self.client.get(url_for('api.run_service')
self.assertTrue(response == expected_responce)

参见 Where to patch section :

The basic principle is that you patch where an object is looked up, which is not necessarily the same place as where it is defined.

关于python - 要修补哪个模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29755302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com