相关疑难解决方法(0)

Python模拟具有不同结果的多个调用

我希望能够对特定属性函数进行多次调用,为每次连续调用返回不同的结果.

在下面的例子中,我想在第一次调用时增加返回5,然后在第二次调用时返回10.

例如:

import mock

class A:
    def __init__(self):
        self.size = 0
    def increment(self, amount):
        self.size += amount
        return amount

@mock.patch("A.increment")
def test_method(self, mock_increment):
    def diff_inc(*args):
        def next_inc(*args):
            #I don't know what belongs in __some_obj__
            some_obj.side_effect = next_inc
            return 10
        return 5

    mock_increment.side_effect = diff_inc
Run Code Online (Sandbox Code Playgroud)

下面的页面几乎包含了我需要的所有内容,除了它假定调用者是一个名为"mock"的对象,但这不能被假设.

http://mock.readthedocs.org/en/latest/examples.html#multiple-calls-with-different-effects

python mocking

26
推荐指数
3
解决办法
2万
查看次数

标签 统计

mocking ×1

python ×1