2013-10-02 39 views
5

如何斷言Mock和/或MagicMock上的屬性被訪問?訪問模擬實例上的斷言屬性

例如,

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實際的方法是什麼?

回答

6

您可以使用PropertyMock描述here

例如,

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()