Sub topic 17
Testing Dart Wrappers and Channel Contracts
Assume you know intermediate Dart, Future, Stream, and basic Flutter testing. This chapter tests the Dart side of a platform channel without starting Android or iOS code.
A channel contract is the agreement between Dart and native code. It includes the channel name, method names, argument keys, return types, and error codes. A wrapper test checks that Dart follows this agreement.
Test a MethodChannel wrapper
Flutter's test messenger can replace a native handler with a mock. The mock receives a MethodCall and returns a value or throws a PlatformException.
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
class GreetingApi {
static const channel = MethodChannel('com.example.app/greeting');
Future<String> greet(String name) async {
final result = await channel.invokeMethod<String>('greet', {
'name': name,
});
return result ?? '';
}
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('sends the expected method and arguments', () async {
final messenger =
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
final calls = <MethodCall>[];
messenger.setMockMethodCallHandler(
GreetingApi.channel,
(call) async {
calls.add(call);
return 'Hello, ${call.arguments['name']}';
},
);
final result = await GreetingApi().greet('Mina');
expect(result, 'Hello, Mina');
expect(calls.single.method, 'greet');
expect(calls.single.arguments, {'name': 'Mina'});
messenger.setMockMethodCallHandler(GreetingApi.channel, null);
});
}The test checks more than the returned text. It verifies the exact method name and argument shape. If native code expects userName but Dart sends name, this test exposes the mismatch.
Test failure contracts too. Configure the mock to throw PlatformException(code: 'INVALID_ARGUMENT'), then assert that the wrapper or its caller handles that code correctly.
Test an EventChannel lifecycle
An EventChannel uses listen and cancel control calls. The native side should start its resource when Dart subscribes and release it when the subscription ends.
class SensorApi {
static const channel = EventChannel('com.example.app/sensor');
Stream<double> get values => channel
.receiveBroadcastStream()
.map((value) => (value as num).toDouble());
}
test('sends listen and cancel', () async {
final messenger =
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
const codec = StandardMethodCodec();
final methods = <String>[];
messenger.setMockMessageHandler(SensorApi.channel.name, (message) async {
final call = codec.decodeMethodCall(message!);
methods.add(call.method);
return codec.encodeSuccessEnvelope(null);
});
final subscription = SensorApi().values.listen((_) {});
await Future<void>.delayed(Duration.zero);
await subscription.cancel();
expect(methods, ['listen', 'cancel']);
messenger.setMockMessageHandler(SensorApi.channel.name, null);
});A separate test can verify that numeric payloads become double values. Integration tests should also confirm that Android and iOS actually emit those payloads through the same codec.
Exercise
What should a MethodChannel test assert besides the returned value?
Show answer
It should assert the exact channel, method name, argument keys and values, expected result type, and important platform error codes.