如何在Flutter集成测试中忽略异常并继续测试执行。

3

在运行 Flutter 集成测试时,我遇到了 FirebaseException 被 FLUTTER TEST FRAMEWORK 捕获的问题。

有没有人能帮忙理解如何忽略集成测试中的 FirebaseException 并继续执行其余部分?

我尝试了以下解决方法:

Future<void> restoreFlutterError(Future<void> Function() call) async {
  FlutterError.onError = (FlutterErrorDetails data) {
    if (data.exception is FirebaseException) {
      return;
    }
    FlutterError.presentError(data);
  };
}

并在testWidgets中调用上述方法

testWidgets('test execution', (tester) async {
    await restoreFlutterError(() async {
      app.main();
      await tester.pumpAndSettle(const Duration(seconds: 10));
    });
    ...
    ...
});

但是出现以下错误!

 A test overrode FlutterError.onError but either failed to return it to its original state, or had unexpected additional errors that it could not handle. Typically, this is caused by using expect() before restoring FlutterError.onError.

非常感谢任何帮助!


我遇到了同样的问题。你成功解决了吗? - undefined
1个回答

0
我通过修改FlutterError.onError函数成功地忽略了异常。类似这样的方法对我起到了作用:
 Future<void> ignoreException(Type exceptionType) async {
   final originalOnError = FlutterError.onError!;
  FlutterError.onError = (FlutterErrorDetails details) {
    final currentError = details.exception.runtimeType;
    if (currentError == exceptionType) {
      return;
    }
    originalOnError(details);
  };
}

稍后在testWidgets中这样调用它,根据你想要忽略的异常来决定:
await ignoreException(NetworkImageLoadException);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接