Hamed Rezaee
Hamed Rezaee

Reputation: 7222

Flutter widget test fails for InkWell tap

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>

Upvotes: 7

Views: 2454

Answers (3)

1housand
1housand

Reputation: 616

This is what worked for me, I set ensureVisible() first.

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

Upvotes: -1

Dharmesh Mansata
Dharmesh Mansata

Reputation: 4738

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!

Upvotes: 3

raj kavadia
raj kavadia

Reputation: 1073

Try Waiting after tapping for like 2 seconds.

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

I think it should work since it worked for me.

Upvotes: 0

Related Questions