【发布时间】:2013-07-17 21:02:21
【问题描述】:
我正在使用 Python 的 mock 库。我知道如何按照document 模拟类实例方法:
>>> 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下得到我想要的值。有什么提示吗?
【问题讨论】:
标签: python unit-testing mocking