Flutter 集成测试绑定错误 - 覆盖 FlutterError.onError

问题描述 投票:0回答:1

我尝试使用integration_test为我的flutter应用程序创建一个测试。当我尝试运行测试时出现一些错误。这是终端上的错误:

'package:flutter_test/src/binding.dart': 断言失败:第 810 行 pos 14: '_pendingExceptionDetails != null': 测试覆盖了 FlutterError.onError 但要么失败 ed 将其返回到原始状态,或者出现无法处理的意外其他错误。通常,这是由于在恢复 Flut 之前使用了 Expect() 造成的 terError.onError.

这是我的代码:

void main() {
  testWidgets('Test_Login_Using_Robo', (tester) async {
    app.main();
    await tester.pump();
    await tester.pumpAndSettle(const Duration(seconds: 5));
    final phoneNumberLoginTextField = find.byKey(const Key('phoneNumberTextField'));
    await tester.tap(phoneNumberLoginTextField);
    expect(phoneNumberLoginTextField, findsOneWidget);
    await tester.enterText(phoneNumberLoginTextField, '82220099611');

    await tester.tap(find.byKey(const Key('loginButton')));

    ///Wait user for loggin in
    await tester.pumpAndSettle(const Duration(seconds: 5));

    ///We Expect to see the otp Screen
    expect(find.byType(OTPControllerScreen), findsOneWidget);
  });
}

哪个部分导致了此错误?如何解决这个问题?之前谢谢

integration-testing flutter-integration-test
1个回答
0
投票

当您覆盖

Flutter.onError
并且您的测试不等待原始状态时,就会出现此问题。

因此请检查是否有任何代码

FlutterError.onError =
可能在某些帮助类中。

我在应用程序初始化代码中有以下代码用于巡逻测试

FlutterError.onError = (FlutterErrorDetails data) {
      final e = data.exception;
      if (e is NetworkImageLoadException) {
        debugPrint('Ignoring expected network error: $e');
        return;
      }
      FlutterError.presentError(data);
    };

副作用是,在测试过程中屏幕冻结,在日志中我得到了

'package:flutter_test/src/binding.dart': Failed assertion: line 985 pos 14: '_pendingExceptionDetails != null': 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

Future<void> ignoreNetworkImageLoadException() async {
    final originalOnError = FlutterError.onError!;
    FlutterError.onError = (FlutterErrorDetails data) {
      final e = data.exception;
      if (e is NetworkImageLoadException) {
        debugPrint('Ignoring expected network error: $e');
        return;
      }
      originalOnError(data);
    };
  }

当这返回 Future 时,我需要

await appHelper.ignoreNetworkImageLoadException();

在我的测试中。

© www.soinside.com 2019 - 2024. All rights reserved.