我必须模拟下面显示的方法:
import json
def some_method(self):
output_one = json.loads(varone)
output_two = json.loads(vartwo)
import json
self.stubs = stubout.StubOutForTesting()
self.stubs.Set(json, "loads", lambda *a: output_one)
self.stubs.Set(json, "loads", lambda *a: output_two)
结果是只有 output_two ,因为 output_one 被覆盖。 我应该如何模拟一个方法两次并且每次都期望不同的输出。
答案 0 :(得分:3)
使用side_effect。
docs中的示例:
>>> mock = Mock()
>>> mock.side_effect = [3, 2, 1]
>>> mock(), mock(), mock()
(3, 2, 1)
答案 1 :(得分:0)
您需要将代码移动到两个测试函数中。
def test_output_one():
self.stubs = stubout.StubOutForTesting()
self.stubs.Set(json, "loads", lambda *a: output_one)
... code for the first test here ...
def test_output_two():
self.stubs = stubout.StubOutForTesting()
self.stubs.Set(json, "loads", lambda *a: output_two)
... code for the second test here ...