【发布时间】:2020-12-09 02:26:24
【问题描述】:
我这样称呼 Future:
//main_bloc.dart
...
getData() {
print("getting data");
repository.getDataFromServer().then((result) {
_handleResult(result);
}).catchError((e) {
_handleError(e);
});
}
在运行时,当repository出现异常时,会在catchError中被捕获并正确转发。
但是,当我像这样对那部分代码进行单元测试时:
//prepare
when(mockRepository.getDataFromServer()).thenThrow(PlatformException(code: "400", message: "Error", details: ""));
//act
bloc.getData();
await untilCalled(mockRepository.getDataFromServer());
//assert
verify(mockRepository.getDataFromServer());
catchError 方法未调用,由于 unHandled 异常,测试失败。
我做错了什么?
【问题讨论】:
标签: unit-testing flutter dart future flutter-test