我正在尝试执行类似的操作:
mymock = Mock()
mymock.variable.side_effect = [1,2,3]
print mymock.variable # Should print 1
print mymock.variable # Should print 2
print mymock.variable # Should print 3
但是,我没有得到预期的结果。我错过了什么?
答案 0 :(得分:1)
仅在将属性作为方法调用时才使用side_effect
:
In [3]: mymock = Mock()
In [4]: mymock.variable.side_effect = [1,2,3]
In [5]: mymock.variable()
Out[5]: 1
In [6]: mymock.variable()
Out[6]: 2
In [7]: mymock.variable()
Out[7]: 3
没有类似的功能可以为属性赋值。