例如,
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
的实际方法是什么?
答案 0 :(得分:10)
您可以按照here所述使用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()