【发布时间】:2015-12-29 23:41:06
【问题描述】:
我正在尝试为应用程序编写一些单元测试,并且我使用 python 模拟。我熟悉其他模拟库,直到现在还没有遇到太多麻烦。我正在尝试模拟对父类的 init 块中的属性集的链式调用。这是我需要的示例:
class ApplicationUnderTest:
def __init__(self):
self.attributeBeginningChain = SomeClass(False)
def methodWithChain(self):
object = self.attributeBeginningChain.methodOfSomeClass()
我需要链式调用来引发错误。我尝试通过以下方式解决此问题:
@patch.object(SomeClass(False), 'methodOfSomeClass', side_effect=ErrorClass)
def test_chained_call(self, mock_someclass):
A = ApplicationUnderTest.methodWithChain()
self.assertTrue(mock_someclass.called)
最后一个断言失败了,所以我很确定这不是这样做的方法。我也试过:
@patch('ApplicationUnderTest.attributeBeginningChain')
def test_chained_call(self, mock_someclass):
mock_someclass.methodOfSomeClass.side_effect = ErrorClass
A = ApplicationUnderTest.methodWithChain()
self.assertTrue(mock_someclass.called)
这会引发错误:
AttributeError: package.ApplicationUnderTest does not have the attribute 'attributeBeginningChain'
我无法更改被测代码,所以我的问题是如何模拟对在 _init__ 函数?我读过这是不可能的,但肯定有解决办法吗?我是否可以通过 autospec 以某种方式指示模拟夹具对调用本身而不是属性对象做出反应?
【问题讨论】:
标签: python unit-testing python-mock