首页 > 解决方案 > 如何使用 unittest.mock.patch 模拟 MongoClient(“TypeError:'MongoClient' 对象不可调用”)

问题描述

我正在尝试使用版本替换我MongoClientmongomock版本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

标签: pythonpython-3.xunit-testingmockingpytest

解决方案


推荐阅读