Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter widget testing - how to execute test to "Go Back to Prevoius Screen"

I am writing widget tests in Flutter. I am not able to find any documentation to execute "Go Back to Prevoius Screen" as I do not find any method for that. Please help with some sample "test" code for the same.

like image 818
STeamup Avatar asked Nov 18 '22 03:11

STeamup


1 Answers

You have to mock navigation observer.

  • First create class class MockNavigatorObserver extends Mock implements NavigatorObserver

  • Declare final mockObserver = MockNavigatorObserver(); in your test file.

  • Put your widget inside MaterialApp and add property navigatorObservers: [mockObserver],

  • Finally inside your testWidgets block {} mock like this:

    final mockObserver = MockNavigatorObserver();
    final backIcon = find.byIcon(Icons.arrow_back_ios);
    expect(backIcon, findsOneWidget);
    await tester.tap(backIcon);
    verify(mockObserver.didPop(any, any));
    await tester.pumpAndSettle();
    
like image 155
Quyen Anh Nguyen Avatar answered Dec 05 '22 21:12

Quyen Anh Nguyen