在lib/thing.py
:
class Class(object):
def class_function1(self):
在app/thing.py
:
def function2(class_object):
class_object.class_function1()
在test/test_thing.py
中,我想在lib.thing.Class.class_function1
使用模拟的 Class() 对象调用 function2 时进行修补,以引发一个AttributeError
应该可以test_function2
畅通无阻的 perc。像这样的东西(不起作用):
def test_function2(self):
mocked_class = mock.MagicMock(name="class", spec_set=lib.thing.Class)
with assertRaises(AttributeError):
with patch ('lib.thing.Class.class_function1', side_effect=AttributeError):
function2(mocked_class)