Purpose of MethodCall list in Flutter plugin unit tests

Issue

The default unit test setup when you create a plugin looks like this:

void main() {
  const MethodChannel channel = MethodChannel(
      'com.example/my_plugin');

  setUp(() {
    channel.setMockMethodCallHandler((MethodCall methodCall) async {
      return '42';
    });
  });

  tearDown(() {
    channel.setMockMethodCallHandler(null);
  });

  test('getPlatformVersion', () async {
    expect(await MyPlugin.platformVersion, '42');
  });
}

However, in a lot of the source code I see people using a List<MethodCall> called log. Here is an example:

  test('setPreferredOrientations control test', () async {
    final List<MethodCall> log = <MethodCall>[];

    SystemChannels.platform.setMockMethodCallHandler((MethodCall methodCall) async {
      log.add(methodCall);
    });

    await SystemChrome.setPreferredOrientations(<DeviceOrientation>[
      DeviceOrientation.portraitUp,
    ]);

    expect(log, hasLength(1));
    expect(log.single, isMethodCall(
      'SystemChrome.setPreferredOrientations',
      arguments: <String>['DeviceOrientation.portraitUp'],
    ));
  });

I understand the mocking with setMockMethodCallHandler, but why use a list of MethodCall when you could just use one? If it were just one case I wouldn’t may much attention, but I see the pattern over and over in the source code.

Solution

I believe the point is to verify that the method call handler was triggered exactly once (and therefore added (“logged”) exactly one entry into the List<MethodCall>). If it were simply a MethodCall variable that changed from null to non-null, verifying that it was not triggered multiple times would be less straightforward.

Answered By – jamesdlin

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.