17

如何断言 aMock和/或 a上的属性已MagicMock被访问?

例如,

from unittest.mock import MagicMock

def foo(x):
    a = x.value

m = MagicMock()
foo(m)
m.attr_accessed('value')    # method that does not exist but I wish did; should return True

检查foo尝试访问的实际方法是什么m.value

4

1 回答 1

17

您可以按照此处所述使用 PropertyMock 。

例如,

from unittest.mock import MagicMock, PropertyMock

def foo(x):
    a = x.value

m = MagicMock()
p = PropertyMock()
type(m).value = p
foo(m)
p.assert_called_once_with()
于 2013-10-02T22:00:30.533 回答