简体   繁体   中英

How to write widget test for this widget

My Widget of which i want to write widget test

InkWell(
      key: const Key(kDraftTileKey),
      onTap: () {
        context.read<CreatorHubTapViewModel>().startPublishingFlowAgain(startPublishingFlowAgainPressed: startPublishingFlowAgainPressed);
      },
      child:...
    );

Fucntion which called when user click on a tap

 void startPublishingFlowAgainPressed() {
    widget.viewModel.saveNFT(nft: widget.nft);
    Navigator.of(context).pushNamed(RouteUtil.kRouteHome);
  }

I create a Mock of this viewmodel using Mockito

class CreatorHubTapViewModel extends ChangeNotifier {
  void onViewOnPylons({required VoidCallback onViewOnPylonsPressed}) {
    onViewOnPylonsPressed.call();
  }

  void startPublishingFlowAgain({required VoidCallback startPublishingFlowAgainPressed}) {
    startPublishingFlowAgainPressed.call();
  }
}

Now in test when i try to make a stub on this function it is not calling thenAnswer also not calling Callback itself. Any way to achieve i just want to toggle my variable when this function calls in actual screen.

  when(viewModel.startPublishingFlowAgain(startPublishingFlowAgainPressed: () {
    clicked = true;
  })).thenAnswer((realInvocation) {});

This is how you can test this function.

 when(viewModel.startPublishingFlowAgain(startPublishingFlowAgainPressed: anyNamed("startPublishingFlowAgainPressed"))).thenAnswer((realInvocation) {
        clicked = true;
      });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM