Flutter小部件测试失败:InkWell点击问题

7

我有一个类似于这样的小部件测试,我能够通过actionKey找到小部件,但是在执行tester.tap(...)后,点击测试失败了,并且tapped值为false,我的测试有什么问题?

testWidgets('some test', (WidgetTester tester) async {
  final UniqueKey actionKey = UniqueKey();
  bool tapped = false;

  await tester.pumpWidget(MaterialApp(
    home: Scaffold(
      body: SlidableListItem(
        child: const ListTile(title: Text('item')),
        actions: <Widget>[
          InkWell(
            key: actionKey,
            child: const Text('action'),
            onTap: () => tapped = true,
          ),
        ],
      ),
    ),
  ));

  await tester.tap(find.byKey(actionKey));
  await tester.pump();

  expect(find.byKey(actionKey), findsOneWidget);
  expect(tapped, isTrue); <- failes
});

The following TestFailure object was thrown running a test:
  Expected: true
  Actual: <false>
3个回答

2

小部件的评估对我来说有效。

InkWell InkWellButton() => find
          .byKey(actionKey)
          .evaluate()
          .first
          .widget;                                                                                 

然后对Inkwell进行操作。

InkWellButton().onTap();
await tester.pumpAndSettle();                                                            

希望这能帮助你!

很不幸,它似乎只适用于墨水瓶。 - 1housand

0

尝试在点击后等待大约2秒钟。

      await tester.pump(Duration(seconds: 2));

我认为它应该可以工作,因为它对我有效。


-1

我想通了。你必须先设置ensureVisible()

var foo = find.byKey(actionKey);
await tester.ensureVisible(foo); // <-- here
await tester.tap(foo);
await tester.pumpAndSettle();

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