I'm developing a native plugin and trying to do unit tests. All unit tests will be done in Dart (No native code).
Flutter has a test example of how you can test call method channel from Dart to native using setMockMethodCallHandler.
The problem is I've not found the way to test method channel that calls from native to Dart that uses setMethodCallHandler to handle a call from native.
Here is an example
// main.dart
class Plugin {
static MethodChannel _channel = const MethodChannel('plugin');
Plugin() {
_channel.setMethodCallHandler((call) async {
print("called from native: ${call.method}");
});
}
}
// tests/main_test.dart
void main() {
const MethodChannel _channel = MethodChannel('core.super_router');
Plugin plugin;
setUp(() async {
TestWidgetsFlutterBinding.ensureInitialized();
plugin = Plugin();
});
test("call from native", () async {
_channel.invokeMethod("something");
// This call can't reach the handler in the Plugin
// And there is no method like mockInvokeMethod
});
}