I used url_launcher: ^6.1.0 in my flutter project. I start to write tests for my widgets, but the part of widgets that used the url_launcher method to launch an URL, not worked properly when running the test.
One of the methods that I used inside my Widget is like below method:
Future<void> _onTapLink(String? href) async {
if (href == null) return;
// canLaunchUrl method never return anything when we are calling this function inside flutter test
if (await canLaunchUrl(Uri.parse(href))) {
await launchUrl(Uri.parse(href));
} else {
print('cannot launch url: $href');
}
}
canLaunchUrl method never returns anything when we are calling this function inside the flutter test.
I'm looking for a way to mock the url_launcher package for using inside flutter tests.
To mock url_launcher you may:
dev_dependenciessection in thepubspec.yamlfile.Notice that here
MockPlatformInterfaceMixinmixin is used.url_launcherto use mocked version by setting it inUrlLauncherPlatform.instance: