2

I am implementing a widget test in which a user logs in and is navigated to the next view upon successful authentication. I have checked out several posts, all which suggest setting up a mock of type NavigatorObserver then using it to verify the next view was pushed. In my widget test I have set up the mock as follows:

class MockNavigatorObserver extends Mock implements NavigatorObserver {}

My set up instantiates the mock as follows:

final mockObserver = MockNavigatorObserver();

In my testWidgets function, I have the following:

await tester.pumpWidget(
    MultiProvider(
    providers: [
      ChangeNotifierProvider(create: (_) => SomeProvider()),
      ChangeNotifierProvider(create: (_) => SomeOtherProvider())
    ],
    child: MaterialApp(
      home: LoginView(),
      navigatorObservers: [mockObserver],
    )));

This is the code that executes the authentication process:

await tester.enterText(usernameTextField, 'username');
await tester.enterText(passwordTextField, 'password');
await tester.pumpAndSettle();
await tester.tap(submitButtonFinder);
await tester.pumpAndSettle();
verify(mockObserver.didPush(any, any)); // causes build failure

Every post I have found thus far, suggests using this to verify that a push has occurred:

verify(mockObserver.didPush(any!, any));

However, this fails to build in my code. this first any parameter in the verify call fails to build with the message

The argument type 'Null' can't be assigned to the parameter type 'Route<dynamic>'.

enter image description here

I have verified that the authentication process is properly initiated in the view and the the credential are being passed. All tests pass except for verification that the push to the next view occurred.

Does anyone know the correct way to verify that the route has been pushed?

Thanks!

Pheepster
  • 6,045
  • 6
  • 41
  • 75

1 Answers1

-1

This is wrong:

verify(mockObserver.didPush(any!, any));

Instead do this:

verify(mockObserver.didPush(any(), any()));

And, if its a named route use the name parameter in any

Fred Grott
  • 3,505
  • 1
  • 23
  • 18