VeryGoodOpenSource / mockingjay

A package that makes it easy to mock, test, and verify navigation in Flutter. Created by Very Good Ventures πŸ¦„

Home Page:https://pub.dev/packages/mockingjay

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NavigatorState is mockable?

fernandomiyahiradb1 opened this issue Β· 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 => {});
commented

any new? I have the same problem πŸ™πŸ½

Hi πŸ‘‹
Can you please provide a link to a minimal reproduction sample? Thanks!

commented
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?

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!

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 πŸ‘