【发布时间】:2016-01-26 00:31:18
【问题描述】:
在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 中,我想在使用模拟的 Class() 对象调用 function2 时修补lib.thing.Class.class_function1 以引发AttributeError,该AttributeError 应该可以畅通无阻地提高到test_function2。像这样的东西(不起作用):
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)
【问题讨论】:
-
删除补丁并仅设置
mocked_class.class_function1.side_effect = AttributeError会在class_function1被function2命中时正确提高AttributeError,但assertRaises不承认它。嗯。靠近一步或侧身一步......
标签: mocking python-unittest magicmock