我正在使用 Python 的mock
库。我知道如何通过遵循文档来模拟类实例方法:
>>> def some_function():
... instance = module.Foo()
... return instance.method()
...
>>> with patch('module.Foo') as mock:
... instance = mock.return_value
... instance.method.return_value = 'the result'
... result = some_function()
... assert result == 'the result'
但是,尝试模拟类实例变量但不起作用(instance.labels
在以下示例中):
>>> with patch('module.Foo') as mock:
... instance = mock.return_value
... instance.method.return_value = 'the result'
... instance.labels = [1, 1, 2, 2]
... result = some_function()
... assert result == 'the result'
基本上我想得到我想要instance.labels
的some_function
价值。有什么提示吗?