我有一个名为 api 的 Python Flask 蓝图,其中有一个名为 utils 的文件,其中包含一个我试图模拟的函数。该 utils 文件的路径是 app/api/utils.py
,我试图模拟的函数是 is_file_writeable()
.
我正在对位于 app/api/configs.py
的蓝图中的路线进行测试。 .内configs.py
, 我这样导入函数:
from app.api.utils import is_file_writeable
我尝试了以下补丁装饰器:
@patch('app.api.configs.is_file_writeable', return_value=False)
和
@patch('app.api.utils.is_file_writeable', return_value=False)
两者分别返回以下错误:
AttributeError: 'Blueprint' object has no attribute 'configs'
和
AttributeError: 'Blueprint' object has no attribute 'utils'
关于为什么我收到导入错误的任何想法?
最佳答案
当我遇到这个问题时,是因为 api
是一个蓝图。而不是寻找 configs.py
文件夹下api
mock 者正在寻找一种叫做 utils
的东西在蓝图中api
.
为了解决这个问题,我导入了 py 文件并直接修补了对象:
from app.api import utils
@patch.object(utils, 'is_file_writeable', lambda x: False)
关于python - 在 Python Flask 蓝图中模拟函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35616545/