6

I have a widget test like this, I can find the widget with actionKey, but the tapping test fails and tapped value is false after tester.tap(...), what is wrong with my test?

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>
Hamed
  • 5,867
  • 4
  • 32
  • 56

3 Answers3

2

Evaluate of widget works for me.

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

and then action on Inkwell works.

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

Hope this helps!

Dharmesh Mansata
  • 4,422
  • 1
  • 27
  • 33
0

Try Waiting after tapping for like 2 seconds.

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

I think it should work since it worked for me.

raj kavadia
  • 926
  • 1
  • 10
  • 30
0

I figured it out. You have to set ensureVisible() first.

var foo = find.byKey(actionKey);
await tester.ensureVisible(foo); // <-- here
await tester.tap(foo);
await tester.pumpAndSettle();
1housand
  • 508
  • 7
  • 16