python - 如何使用 unittest.mock.patch 模拟 MongoClient(“TypeError:'MongoClient' 对象不可调用”)
问题描述
我正在尝试使用版本替换我MongoClient
的mongomock
版本unittest.mock.patch
,但由于某种原因,它会中断。这是我的示例代码,运行pytest -sv
(可以看出,该对象显然是可调用的,所以我真的没有得到错误):
import pymongo
import mongomock
import unittest
class A:
def __init__(self):
self.client = pymongo.MongoClient()
def test_mock():
with unittest.mock.patch('pymongo.MongoClient', new_callable=mongomock.MongoClient):
assert isinstance(A().client, mongomock.MongoClient)
def test_sanity():
assert isinstance(A().client, pymongo.MongoClient)
assert hasattr(mongomock.MongoClient, '__call__')
这是失败:
====================================================================================================================================== FAILURES =======================================================================================================================================
______________________________________________________________________________________________________________________________________ test_mock ______________________________________________________________________________________________________________________________________
def test_mock():
with unittest.mock.patch('pymongo.MongoClient', new_callable=mongomock.MongoClient):
> assert isinstance(A().client, mongomock.MongoClient)
test_mock_mongo.py:13:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <test_mock_mongo.A object at 0x7f74013c7ac0>
def __init__(self):
> self.client = pymongo.MongoClient()
E TypeError: 'MongoClient' object is not callable
test_mock_mongo.py:8: TypeError
=============================================================================================================================== short test summary info ===============================================================================================================================
FAILED test_mock_mongo.py::test_mock - TypeError: 'MongoClient' object is not callable
============================================================================================================================= 1 failed, 1 passed in 0.17s =============================================================================================================================
编辑:我想我必须在导入之前打补丁。我已经修改了代码,但仍然失败:
import mongomock
导入单元测试
使用 unittest.mock.patch('pymongo.MongoClient', new_callable=mongomock.MongoClient) 作为 mocked_mongo: 导入 pymongo 类 A: def init (self): self.client = pymongo.MongoClient()
def test_mock():
assert isinstance(A().client, mongomock.MongoClient)
def test_sanity():
assert isinstance(A().client, pymongo.MongoClient)
assert hasattr(mongomock.MongoClient, '__call__')
新的失败:对象还是pymongo.MongoClient...
蟒蛇3.8.0
pytest 6.0.2
pymongo==3.11.0
mongomock==3.21.0
解决方案
推荐阅读
- mysql - Sequelize & mysql:自动添加子句`[tableName].deleted_at IS NULL`
- azure - 用于将 Azure SQL 数据库连接到 Power BI 的凭据
- sql - 高效的 Django 查询过滤前 n 条记录
- python - Tensorboard 只生成纪元日志,而不是训练/验证
- python - Python Docx、Python XML 兼容性
- python - Youtube-dl KeyError:“条目”
- ruby-on-rails - 从 Milia 切换到 Apartment - 保留数据?
- powershell - PowerShell - 如何将内容添加到 CSV 文件中的特定单元格
- amazon-cognito - 如何正确存储 CognitoUser?
- java - 如何阅读 Discord 嵌入消息?(不和谐 Java JDA)