Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 已访问模拟实例上的Assert属性_Python_Unit Testing_Mocking - Fatal编程技术网

Python 已访问模拟实例上的Assert属性

Python 已访问模拟实例上的Assert属性,python,unit-testing,mocking,Python,Unit Testing,Mocking,我如何断言a和/或a上的属性已被访问 比如说, 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的实际方法是什么?您可以按照说明使用PropertyMo

我如何断言a和/或a上的属性已被访问

比如说,

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
的实际方法是什么?

您可以按照说明使用PropertyMock

e、 g

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