Testing a tap that triggers a SnackBar
widget-test interaction flow.
pumpWidget the app, find the button, tester.tap it, call pump or pumpAndSettle to advance frames, then expect a SnackBar finder.
forgetting to pump after the tap so the SnackBar never appears.
WHAT THIS TESTS Whether you understand the pump-driven frame model of widget tests and can chain a gesture to a verified visual result.
A GOOD ANSWER COVERS First, build the tree with await tester.pumpWidget, wrapping the screen in a MaterialApp and Scaffold so a SnackBar has a ScaffoldMessenger to attach to. Next locate the target with a Finder such as find.byType(ElevatedButton) or find.text('Save'). Simulate the interaction with await tester.tap(finder), which dispatches a tap gesture. The tap triggers a callback that calls ScaffoldMessenger.of(context).showSnackBar, but nothing repaints until you advance frames, so call await tester.pump to render one frame or await tester.pumpAndSettle to run the entry animation to completion. Finally assert with expect(find.byType(SnackBar), findsOneWidget) or match its text.
COMMON WRONG ANSWERS Forgetting to pump after the tap, so the SnackBar is scheduled but never built. Using pumpAndSettle when a periodic animation never settles, causing a timeout. Not wrapping in MaterialApp or Scaffold, so showSnackBar has no messenger. Asserting before the frame advances.
LIKELY FOLLOW-UPS Difference between pump and pumpAndSettle. Why does a SnackBar need a duration consideration in tests. How do you tap by key. How do you test dismissal.
ONE CONCRETE EXAMPLE In testWidgets you call await tester.pumpWidget(MaterialApp(home: MyForm())). Then await tester.tap(find.text('Submit')). Then await tester.pump() to let the SnackBar appear. Then expect(find.text('Saved successfully'), findsOneWidget). If you drop the pump line, the test fails because the showSnackBar rebuild has not been flushed to a frame yet.
Read the original → api.flutter.dev
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.