【问题标题】:Catch an exception in the test code using flutter integration driver使用颤振集成驱动程序在测试代码中捕获异常
【发布时间】:2021-07-13 10:47:43
【问题描述】:

我正在使用 Flutter 集成驱动程序来编写 Flutter Web 应用程序的测试用例。页面加载时,系统抛出异常“ImageCodeException: Failed to decode image data”。但是当我执行开发代码本身时,该异常被捕获。

void main(){

IntegrationTestWidgetsFlutterBinding.ensureInitialization();

group('Test scenario .....',(){
testWidgets('Home',(WidgetTester tester)asynsc{

app.main(); // exception throws calling this line
tester.pumpAndSettle();

});

});

尝试/捕捉

void main(){
    
    IntegrationTestWidgetsFlutterBinding.ensureInitialization();
    
    group('Test scenario .....',(){
    testWidgets('Home',(WidgetTester tester)asynsc{
    
try{
    app.main(); // exception throws calling this line
catch(Exception){}
    tester.pumpAndSettle();
    
    });
    
    });

tester.takeException

void main(){
    
    IntegrationTestWidgetsFlutterBinding.ensureInitialization();
    
    group('Test scenario .....',(){
    testWidgets('Home',(WidgetTester tester)asynsc{
    
tester.takeException()  // or
    app.main(); // exception throws calling this line
tester.takeException()  // or
    tester.pumpAndSettle();
    
    });
    
    });

我尝试了 try/catch , tester.takeException()。但他们没有工作。请问如何在测试代码中捕获异常?

异常详情:widgetTester.Exception被调用,上面的异常将被忽略等等。

【问题讨论】:

    标签: flutter dart flutter-dependencies flutter-web flutter-test


    【解决方案1】:

    你应该重写 FlutterError.onError。下面的示例允许您使用某些消息捕获 FlutterError,可以将其更改为任意异常:

      FlutterError.onError = (FlutterErrorDetails data) {
        if (data.exception is FlutterError) {
          final fErr = data.exception as FlutterError;
          if (fErr.message == '...') {
            // do not forward to presentError
            return;
          }
        }
        FlutterError.presentError(data);
      };
    

    【讨论】:

      猜你喜欢
      • 2021-10-23
      • 2020-06-30
      • 1970-01-01
      • 2020-10-23
      • 2022-08-11
      • 1970-01-01
      • 2020-01-20
      • 2019-09-28
      • 2019-03-14
      相关资源
      最近更新 更多