【问题标题】:Dart Mockito when first, second, ... n calls, return different valueDart Mockito 在第一次,第二次,... n 调用时,返回不同的值
【发布时间】:2020-06-07 15:49:18
【问题描述】:

在 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 中实现同样的效果。我找不到那个文档

【问题讨论】:

标签: flutter dart mockito


【解决方案1】:

您可以在此处阅读 mockito 文档: https://pub.dev/documentation/mockito/latest/ 解决方案是使用您想要的响应的有序数组。以您为例:

final responses = ["You failed", "Success"];
when(myMock.doTheCall()).thenAnswer((_)=>responses.removeAt(0))

这也适用于异步响应(将异步字添加到方法 thenAnswer(...))。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-09
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 2020-10-01
    相关资源
    最近更新 更多