问题 Python模拟类实例变量


我正在使用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 得到我想要的价值。任何提示?


1937
2017-07-18 18:42


起源



答案:


这个版本 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]
>>>

对我来说你的代码  加工。


15
2017-07-18 19:08



它不起作用。我得到了相同的结果 instance.labels = [1, 1, 2, 2],这个被模拟的变量没有得到使用 some_function。在文档中,它是模拟方法而不是变量。 - clwen
更新了我的回答。现在我迷失了,因为你的代码正在运行。 - twil
在我的代码中, labels 仅在调用某个函数后出现。并且在我想要测试的函数中调用该函数。也许这就是原因。我最终模拟了类的初始化,以便它返回我想要的行为的模拟对象。 - clwen
我想在dule.Foo实例创建时引发异常。所以我尝试了mock.side_effect和mock.return_value。两者都没用。为什么? - Hussain
这是一个不同的问题,没有我们可以看到的代码,但我想这个问题就是你要找的 stackoverflow.com/questions/28305406/... - twil


答案:


这个版本 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]
>>>

对我来说你的代码  加工。


15
2017-07-18 19:08



它不起作用。我得到了相同的结果 instance.labels = [1, 1, 2, 2],这个被模拟的变量没有得到使用 some_function。在文档中,它是模拟方法而不是变量。 - clwen
更新了我的回答。现在我迷失了,因为你的代码正在运行。 - twil
在我的代码中, labels 仅在调用某个函数后出现。并且在我想要测试的函数中调用该函数。也许这就是原因。我最终模拟了类的初始化,以便它返回我想要的行为的模拟对象。 - clwen
我想在dule.Foo实例创建时引发异常。所以我尝试了mock.side_effect和mock.return_value。两者都没用。为什么? - Hussain
这是一个不同的问题,没有我们可以看到的代码,但我想这个问题就是你要找的 stackoverflow.com/questions/28305406/... - twil