mockingjay icon indicating copy to clipboard operation
mockingjay copied to clipboard

NavigatorState is mockable?

Open fernandomiyahiradb1 opened this issue 4 years ago • 4 comments

Is there any form to mock NavigatorState? In my navigations tests with Navigator.of(context) the mock with mockingjay works fine, but some parts of my application uses NavigatorState and the mocks are not working as expected.

Future pushNamed(String routeName, {Object? arguments}) async => await _getCurrentState()!.pushNamed(routeName, arguments: arguments);

The _getCurrentState just return a NavigatorState.

When i run the test the behavior pushNamed are not overridden as expected.

var mockNavigator = MockNavigator();
when(() => mockNavigator.pushNamed(any())).thenAnswer((_) async => {});

fernandomiyahiradb1 avatar Jul 27 '22 15:07 fernandomiyahiradb1

any new? I have the same problem 🙏🏽

pedroculque avatar Oct 06 '22 16:10 pedroculque

Hi 👋 Can you please provide a link to a minimal reproduction sample? Thanks!

felangel avatar Oct 10 '22 18:10 felangel

class NavigationService {
  static final NavigationService _instance = NavigatorService._internal();
  static NavigatorService get instance => _instance;
  final GlobalKey<NavigatorState> navigatorKey =
      new GlobalKey<NavigatorState>();
  Future<dynamic> navigateTo(String routeName) {
    return navigatorKey.currentState.pushNamed(routeName);
  }
}

Here no app I use navigatorKey to navigate the bottom navigation bar, when you try to test using mockingjay it does not work

MaterialApp(
              title: 'Example',
              navigatorKey: NavigatorService.instance.navigatorKey,
                home: Scaffold(
                      body: Container(),
                    ),);

@felangel can you show an example of how to mock in this scenario?

pedroculque avatar Oct 10 '22 19:10 pedroculque

Hi @pedroculque can you please provide a link to a complete reproduction sample with a failing test so I can run/debug the example locally? Thanks!

felangel avatar Oct 14 '22 16:10 felangel

Closing for now since it's hard to help without a complete reproduction sample. Feel free to provide one and I'm happy to take a closer look 👍

felangel avatar Oct 27 '22 22:10 felangel