Sub topic 11
Testing Code That Depends on Generated APIs
This chapter assumes that you know basic Dart tests, Future, and dependency injection. It also assumes that Pigeon has generated a Dart client for a native HostApi.
Pigeon removes many channel mistakes, but generated code still crosses a platform boundary. A normal unit test should not start Android or iOS code. Instead, test your Dart code with a fake implementation, then use a smaller integration test to verify the generated channel wiring.
Separate app logic from the generated client
Suppose Pigeon generated a BatteryApi client. Calling it directly throughout the app makes tests depend on a binary messenger and platform setup:
final api = BatteryApi();
final info = await api.getBatteryInfo();Put the generated client behind a small Dart interface. This interface is an abstraction, which is a simplified contract that hides implementation details.
import 'src/messages.g.dart';
abstract interface class BatteryReader {
Future<BatteryInfo> read();
}
class PigeonBatteryReader implements BatteryReader {
PigeonBatteryReader(this._api);
final BatteryApi _api;
@override
Future<BatteryInfo> read() => _api.getBatteryInfo();
}
class BatteryPresenter {
BatteryPresenter(this._reader);
final BatteryReader _reader;
Future<String> label() async {
final battery = await _reader.read();
return '${battery.level}% (${battery.state.name})';
}
}The application depends on BatteryReader, not directly on Pigeon. Production code supplies PigeonBatteryReader. A test supplies a fake.
Unit-test behavior with a fake
A fake is a test implementation that returns controlled values. It lets the test check your application behavior without requiring a device.
import 'package:flutter_test/flutter_test.dart';
import 'package:my_app/battery.dart';
import 'package:my_app/src/messages.g.dart';
class FakeBatteryReader implements BatteryReader {
FakeBatteryReader(this.result);
BatteryInfo result;
@override
Future<BatteryInfo> read() async => result;
}
void main() {
test('formats the battery level and state', () async {
final reader = FakeBatteryReader(
BatteryInfo(level: 82, state: BatteryState.discharging),
);
final presenter = BatteryPresenter(reader);
expect(await presenter.label(), '82% (discharging)');
});
}This test is fast and stable because it checks only BatteryPresenter. It does not check whether Android reports the correct battery level. That responsibility belongs to native tests or an integration test.
Test failure behavior too. Your fake can throw the same kind of error that the generated client may surface from native code.
class FailingBatteryReader implements BatteryReader {
@override
Future<BatteryInfo> read() {
return Future.error(StateError('Battery service unavailable'));
}
}Your production layer should decide how such errors become user-visible states. Keep that decision outside the generated API adapter when possible.
Test channel wiring separately
A generated client uses Flutter's binary messenger to send encoded messages. A messenger test replaces the native side with a handler. This verifies channel names, codecs, method arguments, and returned values.
The exact channel names and handler shapes belong to the generated file. Prefer the generated Pigeon test helpers when your Pigeon version provides them. If you install a messenger handler manually, treat the generated output as the source of truth and update the test whenever the schema changes.
| Test kind | What it verifies | Needs a device |
|---|---|---|
| Unit test with a fake | Dart application behavior | No |
| Messenger test | Generated Dart channel wiring | No |
| Integration test | Registration and real native behavior | Usually |
Keep generated files and fakes synchronized
A fake must implement the current application-facing interface. If a Pigeon method changes from synchronous to asynchronous, update the adapter and fake together. If a data class gains a required field, compiler errors should identify every affected test.
For native implementations, add platform tests for parsing, permissions, and system API failures. Use an integration test only for the boundary that unit tests cannot cover, such as whether BatteryApi.setUp was registered on the active Flutter engine.
Exercise
Why should a unit test depend on BatteryReader instead of constructing BatteryApi directly?
Show answer
BatteryReader lets the test provide a fake, so the test checks Dart behavior without a platform messenger, device, or native implementation.