创建像 class 对象一样的 MagicMock,并在实例化时创建一个 MagicMock 实例

Create MagicMock that acts like a class object and when instantiated creates a MagicMock instance

我有一个很有趣的问题。我正在尝试模拟一个 class 将通过内部验证并且在实例化时将 return 一个 magicmock 实例。

例如,我有一个验证字段的 pydantic 模型:

from typing import Type
from pydantic import BaseModel

class SomeClass:
    pass

class SubSomeClass(SomeClass):
    pass

class OtherClass:
    pass

class SomeModel(BaseModel):
    cls: Type[SomeClass]

# here I can put either `SomeClass` or its subclasses
validated_some = SomeModel(cls=SubSomeClass)

# this would cause ValidationError
SomeModel(cls=OtherClass)
"""
pydantic.error_wrappers.ValidationError: 1 validation error for SomeModel
cls
  subclass of SomeClass expected (type=type_error.subclass; expected_class=SomeClass)
"""

现在在我的测试中,我正在尝试使用 MagicMock 模拟我的 SomeClass,但我无法创建模拟的 class 对象 这就像 SomeClass.

的子 class

我尝试了以下方法:

>>> type(Mock(spec=SomeClass))
<class 'unittest.mock.Mock'> # doesn't pass validation
class MockSomeClass(SomeClass, MagicMock):
    """Mocked SomeClass"""

    def __init__(self, *args, **kwargs):
        pass

实例化时 return MagicMock 实例化即 MockSomeClass().

基本上我需要这样的东西:

>>> type(mocked_some_class)
<class 'type'>
>>> import inspect
>>> inspect.getmro(mocked_some_class)
(..., <class '__main__.SomeClass'>, ...)
>>> mocked_some_class()
<MagicMock id='...'>

这是我最后做的:

from unittest.mock import Mock

class MockSomeClass(SomeClass, Mock):
    """Mocked SomeClass class"""

    def __new__(cls, *args, **kwargs):
        return MagicMock()

现在所有检查都通过了:

>>> type(MockSomeClass)
<class 'type'>

>>> inspect.getmro(MockSomeClass)
(<class '__main__.MockSomeClass'>, <class '__main__.SomeClass'>, <class 'unittest.mock.Mock'>, ...)

>>> MockSomeClass()
<MagicMock id='...'>

希望对某人有用! :)