我使用的模拟库是... mock .
The mocking library I use is ... mock.
当我尝试为某个功能(旧版代码)编写测试用例时,遇到了这个模拟嵌套功能"问题.
I came across this "mock nested functions" problem when I tried to write test case for a function(legacy code).
此函数使用了非常复杂的嵌套函数,对其他模块的依赖性很大.
This function used a very complex nested function with heavy dependencies on other modules.
我想知道是否可以用mock模拟嵌套函数.
I wonder if it's possible to mock nested functions with mock.
推荐答案例如,您需要模拟来自Google DRIVE API的嵌套函数调用(链接函数)
for example you need to mock nested function calls (chained functions) from Google DRIVE API
result = get_drive_service().files().insert(body='body', convert=True).execute()因此,您需要通过以下功能进行修补:service_mock(),files(),insert(),直到最后一次execute()响应:
so you need to patch through functions: service_mock(), files(), insert(), till last execute() response:
from mock import patch with patch('path.to.import.get_drive_service') as service_mock: service_mock.return_value.files.return_value.insert.\ return_value.execute.return_value = {'key': 'value', 'status': 200}主要方案: return_value .第二. return_value .第三. return_value .last. return_value = rsp
Main scheme: first.return_value.second.return_value.third.return_value.last.return_value = rsp
更多推荐
如何模拟嵌套函数?
发布评论