如何使用 abc abstract base class 作为模拟规范?

How to use abc abstract base class as mock spec?

我有一个抽象基础class:

import abc
import six

@six.add_metaclass(abc.ABCMeta)
class A(object):

    @abc.abstractmethod
    def f(self, arg1):
        pass

我想用这个 class 作为模拟的规范。

import mock
mock_a = mock.Mock(spec=A)

部分有效。例如,mock_a.x 结果为 AttribureError("Mock object has no attribute 'x')。但是 mock_a.f 并未根据 A.f 中的抽象方法进行指定。无论传递给 f.

的参数数量如何,它 returns 都是一个模拟
mock_a = mock.Mock(spec=A)

# Succeeds
print mock_a.f(1)

# Should fail, but returns a mock
print mock_a.f(1,2)

# Correctly fails
print mock_a.x

Mock 可以从 A.fcreate_autospec ...

创建一个模拟
f_spec = mock.create_autospec(A.f)

# Succeeds
f_spec(mock_a, 1)

# Correctly fails
f_spec(mock_a, 1, 2)

... 但对于 A

的属性不这样做

如何创建准确实现抽象基础的 mock class?

mock.create_autospec()应用于class:

>>> mock_a = mock.create_autospec(spec=A)
>>>
>>> # Should succeed
>>> print mock_a.f(1)
<MagicMock name='mock.f()' id='140401932347984'>
>>>
>>> # Should fail
>>> print mock_a.f(1,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/mock/mock.py", line 1061, in __call__
    _mock_self._mock_check_sig(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/mock/mock.py", line 227, in checksig
    sig.bind(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/mock/mock.py", line 95, in fixedbind
    return self._bind(args, kwargs)
  File "/usr/lib/python2.7/dist-packages/funcsigs/__init__.py", line 712, in _bind
    raise TypeError('too many positional arguments')
TypeError: too many positional arguments
>>>
>>> # Should fail
>>> print mock_a.x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/mock/mock.py", line 716, in __getattr__
    raise AttributeError("Mock object has no attribute %r" % name)
AttributeError: Mock object has no attribute 'x'
>>>