python-3.x - 属性模拟不返回 Mock.return_value 集
问题描述
在测试文件中,我模拟了这样定义property
的类的属性:MyClass
path.to.my.module
# test file
import unittest
from app.module import run_service
class TestMyService(unittest.TestCase):
def setUp(self):
self.service_url = ...
self.query = ...
self.headers = ....
def test_my_service(self):
proc = Process(target=run_service)
proc.start()
with patch("path.to.my.module.MyClass.property", new_callable=PropertyMock) as attr:
attr.return_value = "expected_value"
res = requests.get(self.service_url, self.query, headers=self.headers)
self.assertEqual(res.status_code, 200)
proc.join()
在实际执行中,property
从外部服务 X 下载一些东西。服务的 GET 导入MyClass
,实例化它,并调用prs
在第三个模块中定义的函数,该函数具有类型的参数MyClass
。prs
调用property
其参数的属性。
当我运行上面的测试文件时,回溯会到达prs
调用位置MyClass.property
并尝试实际执行下载。我原以为MyClass.property
会回来"expected_value"
。我怎样才能做到这一点?
解决方案
推荐阅读
- json - 执行复杂的 jsonpath 查询
- javascript - 窗口添加的属性返回未定义
- ios - 快速转换数字价格
- salesforce - 我想在 Salesforce 中编写一个可以执行以下任务的公式
- python - 阈值处理后的 imshow(Scikit Image)问题
- delphi - 如何以编程方式在 TBCEditor 中设置文本选择?
- firebase - 我是 Flutter Web 的新手,如何使用 Firebase 电话身份验证对用户进行身份验证,有没有办法让用户保持登录状态?
- javascript - React Leaflet:从 MongoDB 获取对象到 Leaflet 映射作为标记(使用 node.js 和 express)
- aem - AEM - 经典 UI - 我们可以在经典 UI 对话 CQ 小部件上实现工具提示吗?如果可能,我们该怎么做?
- nginx - 带有 2 台服务器的 nginx 反向代理