python中init外面的属性_Python mock属性在\uyu init中定义和设置__
我正在嘗試為一個應用程序編寫一些單元測試,我使用python mock。我對其他嘲弄庫很熟悉,直到現在也沒遇到過什么麻煩。我試圖模擬對父類的init塊中的屬性集的鏈式調用。以下是我需要的示例:class ApplicationUnderTest:
def __init__(self):
self.attributeBeginningChain = SomeClass(False)
def methodWithChain(self):
object = self.attributeBeginningChain.methodOfSomeClass()
我需要鏈式調用來拋出錯誤。我試圖通過以下方式解決這個問題:
^{pr2}$
最后一個斷言失敗了,所以我很確定這不是這樣做的方法。我也嘗試過:@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'
我不能對測試中的代碼進行更改,所以我的問題是如何模擬對
_初始化函數?我已經讀到這是不可能的,但肯定有一個解決辦法?我是否可以通過autospec命令模擬fixture響應調用本身而不是屬性對象?在
總結
以上是生活随笔為你收集整理的python中init外面的属性_Python mock属性在\uyu init中定义和设置__的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: react-native如何解决Andr
- 下一篇: 企业如何架设代理服务器联接互联网