Solution: Quality Assurance
Explore solutions to the widget testing challenge.
Challenge solutions
Great job on completing all the steps in the previous challenge! Feel free to compare your code solutions with the solutions below:
Solution 1: Test if the tasks are visible
-
Pump the
TasksList
widget in readiness for testing.// Build the widget to test await tester.pumpWidget( MaterialApp(home: Scaffold(body: tasksListWidget)), );
-
Use finder functions to confirm that tasks are shown on screen.
// Verify that tasks are shown expect(find.text('Task 1'), findsOneWidget); expect(find.text('Task 2'), findsOneWidget); expect(find.text('Task 3'), findsOneWidget);
Solution 2: Test if the tasks can be dismissed
-
Drag a task to dismiss it from the screen.
// Drag the task to dismiss it await tester.drag( find.ancestor( of: find.text('Task 2'), matching: find.byWidgetPredicate((w) => w is CheckboxListTile), ), const Offset(500, 0), );
-
Wait till the drag to dismiss action is completed.
// Wait for the task to be dismissed and delete confirmation SnackBar shown await tester.pumpAndSettle();
-
Test that the task has been removed from the screen and a
SnackBar
has been shown to the user, confirming that the task is deleted.// Verify that the task is deleted and a SnackBar is shown expect(find.text('Task 2'), findsNothing); final snackBarFinder = find.byType(SnackBar); expect(snackBarFinder, findsOneWidget);
Final app
Compare your final app with the app below:
Get hands-on with 1400+ tech skills courses.