Python 如何使用unittest.mock.patch模拟MongoClient(“TypeError:”MongoClient&“x27”对象不可调用“)
我试图使用Python 如何使用unittest.mock.patch模拟MongoClient(“TypeError:”MongoClient&“x27”对象不可调用“),python,python-3.x,unit-testing,mocking,pytest,Python,Python 3.x,Unit Testing,Mocking,Pytest,我试图使用unittest.mock.patch将我的MongoClient替换为mongomock版本,但由于某种原因,它出现了故障。下面是我的示例代码,与pytest-sv一起运行(可以看到,该对象显然是可调用的,因此我确实没有得到错误): 这就是失败: ========================================================================================================================
unittest.mock.patch
将我的MongoClient
替换为mongomock
版本,但由于某种原因,它出现了故障。下面是我的示例代码,与pytest-sv
一起运行(可以看到,该对象显然是可调用的,因此我确实没有得到错误):
这就是失败:
====================================================================================================================================== 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 =============================================================================================================================
导入单元测试
将unittest.mock.patch('pymongo.MongoClient',new\u callable=mongomock.MongoClient)作为mock\u mongo:
进口pymongo
A类:
def初始化(自身):
self.client=pymongo.MongoClient()
新失败:对象仍然是pymongo.MongoClient
python 3.8.0
pytest 6.0.2
pymongo==3.11.0
mongomock==3.21.0我认为您应该模拟您的实例,而不是类定义,因此,寻找模拟A.client而不是
new\u callable
的MongoClient在这里是错误的:patch('pymongo.MongoClient',MongoClient)
应该已经足够了。
====================================================================================================================================== 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
def test_mock():
assert isinstance(A().client, mongomock.MongoClient)
def test_sanity():
assert isinstance(A().client, pymongo.MongoClient)
assert hasattr(mongomock.MongoClient, '__call__')