在 Java Mockito 中,您可以像这样为每次调用模拟不同的返回值:
when(myMock.doTheCall())
.thenReturn("You failed")
.thenReturn("Success");
或者像这样:
when(myMock.doTheCall()).thenReturn("Success", "you failed");
来源:Simulate first call fails, second call succeeds
我想知道如何在 Dart Mockito 中实现相同的目标。我找不到相关的文档
最佳答案
您可以在此处阅读 mockito 文档: https://pub.dev/documentation/mockito/latest/ 解决方案是使用您想要的响应的有序数组。对于您的示例:
final responses = ["You failed", "Success"];
when(myMock.doTheCall()).thenAnswer((_)=>responses.removeAt(0))
这也适用于异步响应(将异步字添加到方法 thenAnswer(...) 中)。
关于flutter - Dart Mockito 第一次,第二次,... n 调用时,返回不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62247857/