Flutter: testing a method channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
import 'package:url_launcher/url_launcher.dart'; | |
class AppStoreLauncherException implements Exception { | |
final String msg; | |
const AppStoreLauncherException(this.msg) : super(); | |
} | |
class AppStoreLauncher { | |
const AppStoreLauncher() : super(); | |
Future<void> launchWebStore({bool isAndroid = true}) async { | |
String url = 'https://apps.apple.com/...'; | |
if (isAndroid) { | |
url = 'https://play.google.com/store/apps/details?id=...'; | |
} | |
if (await canLaunch(url)) { | |
await launch(url); | |
} else { | |
throw AppStoreLauncherException('Could not launch $url'); | |
} | |
} | |
} | |
*/ | |
import 'package:flutter/services.dart'; | |
import 'package:flutter_test/flutter_test.dart'; | |
import 'package:ssu_life/features/splash/domain/helper/app_store_launcher.dart'; | |
void main() { | |
late List<MethodCall> fakeLog; | |
late MethodChannel mockChannel; | |
late AppStoreLauncher launcher; | |
void _mockChannelValues({bool canLaunch = true, bool launch = true}) { | |
TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.setMockMethodCallHandler(mockChannel, | |
(MethodCall methodCall) async { | |
fakeLog.add(methodCall); | |
if (methodCall.method == "canLaunch") { | |
return canLaunch; | |
} else if (methodCall.method == "launch") { | |
return launch; | |
} | |
return false; | |
}); | |
} | |
setUp(() { | |
fakeLog = <MethodCall>[]; | |
mockChannel = const MethodChannel('plugins.flutter.io/url_launcher'); | |
launcher = const AppStoreLauncher(); | |
TestWidgetsFlutterBinding.ensureInitialized(); | |
}); | |
tearDown(() { | |
fakeLog = <MethodCall>[]; | |
}); | |
test('When launching android store both canLaunch and launch are invoked', () async { | |
_mockChannelValues(canLaunch: true, launch: true); | |
await launcher.launchWebStore(isAndroid: true); | |
expect(fakeLog.length, 2); | |
expect( | |
fakeLog.map((e) => e.method), | |
equals(<String>[ | |
'canLaunch', | |
'launch', | |
])); | |
}); | |
test('When launching ios store both canLaunch and launch are invoked', () async { | |
_mockChannelValues(canLaunch: true, launch: true); | |
await launcher.launchWebStore(isAndroid: false); | |
expect(fakeLog.length, 2); | |
expect( | |
fakeLog.map((e) => e.method), | |
equals(<String>[ | |
'canLaunch', | |
'launch', | |
])); | |
}); | |
test('When launching android only canLaunch invoked', () async { | |
_mockChannelValues(canLaunch: false, launch: true); | |
final actual = launcher.launchWebStore; | |
expect(() async => actual.call(isAndroid: true), throwsA(isA<AppStoreLauncherException>())); | |
expect(fakeLog.length, 1); | |
expect( | |
fakeLog.map((e) => e.method), | |
equals(<String>[ | |
'canLaunch', | |
])); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment