Testing in Flutter

Sep 19 2023 · Dart 2.19.3, Flutter 3.7.6, Android Studio 2021.3.1, Visual Studio Code 1.7.4

Part 3: Write Widget Tests

12. Mock Quotes Data & Override Riverpod Dependencies

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 11. Test Different Login Scenarios Next episode: 13. Test Quotes Page

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

The App navigates automatically to the Quotes page after the user logs in. So we will test if the App navigates to the Quotes page after the user taps the login button with the correct credentials.

await tester.pumpAndSettle();
var quotesPageTitle = find.text('All Quotes');
expect(quotesPageTitle, findsOneWidget);
Navigator.push(
  context,
  MaterialPageRoute(
    builder: (_) => AllQuotesScreen(),
  ),
);
class MockQuotesService extends Mock implements QuotesService {}
MockQuotesService mockQuotesService = MockQuotesService();
await tester.pumpWidget(
  ProviderScope(
    overrides: [
      quotesNotifierProvider
          .overrideWith((ref) => QuotesNotifier(mockQuotesService)),
    ],
    child: MaterialApp(
      home: LoginScreen(),
    ),
  ),
);

when(() => mockQuotesService.getQuotes())
  .thenAnswer((_) async => mockQuotesForTesting);
await tester.pumpAndSettle();