flutter - Dart Mockito 第一次,第二次,... n 调用时,返回不同的值

标签 flutter dart mockito

在 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/

相关文章:

dart - [ flutter/Dart ] : How to dynamically change the keyboard type for a TextFormField in a Form?

flutter null 或空列表

java - Mockito:抛出指定的已检查异常时为 "Checked exception is invalid for this method"

flutter - 从 DropdownItems 中选择值后,DropdownButton 值未更新。如何使用 selectedValue 更新默认值?

flutter - 始终保持选中至少一个复选框

firebase - Firestore查询无法获取文档

dart - Flutter 只渲染硬编码的表情符号。动态构造的表情符号显示为纯文本

java - Mockito - 什么规则管理类似 Collection 类的模拟注入(inject)?

java - 使用 Mockito 模拟在 Grizzly 上运行的 Jersey REST Api

dart - 如何在 Dart 中初始化 mixin 的不可变数据?