【问题标题】:Flutter: Get a PopupMenuButton's PopupMenuItem text in unit testsFlutter:在单元测试中获取一个 PopupMenuButton 的 PopupMenuItem 文本
【发布时间】:2018-09-07 14:29:52
【问题描述】:

我有一个 PaginatedDataTable,它有一个带有 PopupMenuButton 的 DataCell。 WidgetTester 可以找到每个 DataCell 没有问题,但我似乎无法引用 PopupMenuButton 的项目来尝试选择一个。

如何在单元测试中获取 PopupMenuButton 的 PopupMenuItem 文本?我是否正确使用await tester.pump(); 来允许菜单出现?

这是我现在的做法:

...
expect(find.byIcon(Icons.more_horiz).first, findsOneWidget); // works!

await tester.tap(find.byIcon(Icons.more_horiz).first);
await tester.pump();

var byType = find.text('Quote');
expect(byType, findsOneWidget); // fails!

失败了

zero widgets with text "Quote" (ignoring offstage widgets)...

这里是 DataCell 标记

...
new DataCell(...),
new DataCell(new PopupMenuButton<quickActions>(
      icon: new Icon(Icons.more_horiz),
      onSelected: (quickActions action) {
        _selectContextAction(action);
      },
      itemBuilder: (BuildContext context) => <PopupMenuEntry<quickActions>>[
            new PopupMenuItem<quickActions>(
              value: quickActions.edit,
              child: new Text('Edit'),
            ),
            new PopupMenuItem<quickActions>(
              value: quickActions.remove,
              child: new Text('Remove'),
            ),
            new PopupMenuItem<quickActions>(
              value: quickActions.reschedule,
              child: new Text('Re-schedule'),
            ),
            new PopupMenuItem<quickActions>(
              value: quickActions.bid,
              child: new Text('Quote'),
            ),
          ],
    ))

【问题讨论】:

    标签: dart flutter flutter-test


    【解决方案1】:

    我知道这是一个老问题,但我发现自己遇到了同样的问题并且我已经解决了。

    对于那些有同样问题的人,我正在发布我的答案。我的测试和你的有点不同。

    什么是Pump

    在持续时间后触发一帧。

    这使得框架表现得好像应用程序已经卡顿(错过了 帧)持续一段时间,然后接收到垂直同步信号 绘制应用程序。

    什么是PumpAndSettle

    在给定的持续时间内重复调用泵 直到不再安排任何帧。这将调用泵 至少一次,即使在函数运行时没有安排任何帧 调用,以刷新可能自己调度的任何待处理的微任务 一个框架。

    //My Code
    //await tester.tap(find.byIcon(Icons.text_fields));
    //await tester.pumpAndSettle();
    
    //await tester.tap(find.text(expectedFontSize + ' px'));
    //expect(fontSize, expectedFontSize);
    
    //call again if you want to do testing again or something again 
    //await tester.pumpAndSettle();
    
     var mainButton = find.byIcon(Icons.more_horiz); 
     expect(mainButton, findsOneWidget);
    
     await tester.tap(mainButton);
     await tester.pumpAndSettle();
    
     var childButton = find.text('Quote');
     expect(childButton , findsOneWidget); //
    

    对我来说,代码只有在调用 PumpAndSettle 方法后才能工作

    【讨论】:

    • @E.Sun 阅读了黄色引用部分。它基本上作为一个队列工作。当您抽水并安顿下来时,它至少会运行一次以清空并运行队列中的任务。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    • 2022-11-04
    相关资源
    最近更新 更多