Flutter:在单元测试中获取 PopupMenuButton 的 PopupMenuItem 文本
Flutter: Get a PopupMenuButton's PopupMenuItem text in unit tests
我有一个 PaginatedDataTable,它有一个带有 PopupMenuButton 的 DataCell。 WidgetTester 可以毫无问题地找到每个 DataCell,但我似乎无法引用 PopupMenuButton 的项目来尝试 select 一个。
如何在单元测试中获取 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'),
),
],
))
我知道这是一个老问题,但我发现自己遇到了同样的问题并且已经解决了。
对于那些有同样问题的人,我发布了我的答案。我的测试和你的有点不同。
What is Pump
Triggers a frame after duration amount of time.
This makes the framework act as if the application had janked (missed
frames) for duration amount of time, and then received a v-sync signal
to paint the application.
What is PumpAndSettle
Repeatedly calls pump with the given duration
until there are no longer any frames scheduled. This will call pump at
least once, even if no frames are scheduled when the function is
called, to flush any pending microtasks which may themselves schedule
a frame.
//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 方法后才有效
我有一个 PaginatedDataTable,它有一个带有 PopupMenuButton 的 DataCell。 WidgetTester 可以毫无问题地找到每个 DataCell,但我似乎无法引用 PopupMenuButton 的项目来尝试 select 一个。
如何在单元测试中获取 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'),
),
],
))
我知道这是一个老问题,但我发现自己遇到了同样的问题并且已经解决了。
对于那些有同样问题的人,我发布了我的答案。我的测试和你的有点不同。
What is Pump
Triggers a frame after duration amount of time.
This makes the framework act as if the application had janked (missed frames) for duration amount of time, and then received a v-sync signal to paint the application.
What is PumpAndSettle
Repeatedly calls pump with the given duration until there are no longer any frames scheduled. This will call pump at least once, even if no frames are scheduled when the function is called, to flush any pending microtasks which may themselves schedule a frame.
//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 方法后才有效