Переменная экземпляра класса Python mock
Я использую в 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'
в основном я хочу!--3--> под some_function получить значение я хочу. Есть намеки?
1 ответов
эта версия some_function() отпечатки поиздевались labels свойства:
def some_function():
    instance = module.Foo()
    print instance.labels
    return instance.method()
мой module.py:
class Foo(object):
    labels = [5, 6, 7]
    def method(self):
        return 'some'
заплатка такая же, как у вас:
with patch('module.Foo') as mock:
    instance = mock.return_value
    instance.method.return_value = 'the result'
    instance.labels = [1,2,3,4,5]
    result = some_function()
    assert result == 'the result
полный сеанс консоли:
>>> from mock import patch
>>> import module
>>> 
>>> def some_function():
...     instance = module.Foo()
...     print instance.labels
...     return instance.method()
... 
>>> some_function()
[5, 6, 7]
'some'
>>> 
>>> with patch('module.Foo') as mock:
...     instance = mock.return_value
...     instance.method.return_value = 'the result'
...     instance.labels = [1,2,3,4,5]
...     result = some_function()
...     assert result == 'the result'
...     
... 
[1, 2, 3, 4, 5]
>>>
для меня ваш код is работает.