我希望能够对特定属性函数进行多次调用,为每次连续调用返回不同的结果。
在下面的示例中,我希望增量在第一次调用时返回 5,然后在第二次调用时返回 10。
Ex:
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
下面的页面几乎包含了我需要的所有内容,除了假设调用者是一个名为“mock”的对象,但不能假设这一点。
http://mock.readthedocs.org/en/latest/examples.html#multiple-calls-with- Different-effects http://mock.readthedocs.org/en/latest/examples.html#multiple-calls-with-different-effects
您只需将一个可迭代对象传递给副作用,并让它遍历您进行的每个调用的值列表。
@mock.patch("A.increment")
def test_method(self, mock_increment):
mock_increment.side_effect = [5,10]
self.assertEqual(mock_increment(), 5)
self.assertEqual(mock_increment(), 10)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)