Skip to content

How to mock EventChannel in unit test? #38954

Closed

Description

@befovy

I made a flutter media player plugin https://github.com/befovy/fijkplayer.
And used EventChannel and MethodChannel in it.

EventChannel('befovy.com/fijkplayer/event/$id' )
            .receiveBroadcastStream()
            .listen(_eventListener, onError: _errorListener);

receiveBroadcastStream is used in dart side to listen event post from android / iOS native side.
It works fine when running on device.

When I write unit test case, I noticed setMockMethodCallHandler can be used to mock method response from native side. But I can't find any way to mock event post ?

In Shortly,
I post event to dart size from android / iOS native side using EventChannel.EventSink FlutterEventSink . How to mock this in unit test ?
Thanks.

flutter doctor summary

[✓] Flutter (Channel stable, v1.7.8+hotfix.4, on Mac OS X 10.14.3 18D109, locale zh-Hans-CN)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 10.3)
[✓] iOS tools - develop for iOS devices
[✓] Android Studio (version 3.4)
[✓] Android Studio (version 3.5)
[✓] IntelliJ IDEA Ultimate Edition (version 2019.1.2)
[✓] VS Code (version 1.37.1)
[✓] Connected device (1 available)

• No issues found!

Activity

  1. changed the title [-]How to mock EventChannel ?[/-] [+]How to mock EventChannel in unit test?[/+] on Aug 21, 2019
  2. juliocbcotta commented on Aug 21, 2019

    @juliocbcotta

    Not a great solution, but still a solution. here
    EventChannel is backed using a MethodChannel... so do not worry about the link above creating a MethodChannel.

    Flutter could have something similar to MethodChannel. setMockMethodCallHandler.

  3. befovy commented on Aug 21, 2019

    @befovy
    Author

    @BugsBunnyBR 👍 Your answer has resolved my problems. Thanks.

    I known EventChannel is backed using a MethodChannel.

    class EventChannel {
      Stream<dynamic> receiveBroadcastStream([ dynamic arguments ]) {
        final MethodChannel methodChannel = MethodChannel(name, codec);
        StreamController<dynamic> controller;
        controller = StreamController<dynamic>.broadcast(onListen: () async {
          defaultBinaryMessenger.setMessageHandler(name, (ByteData reply) async {
            if (reply == null) {
              controller.close();
            } else {
              try {
                controller.add(codec.decodeEnvelope(reply));
              } on PlatformException catch (e) {
                controller.addError(e);
              }
            }
            return null;
          });
      ...
    }

    The event Stream comes from MessageHandler set in receiveBroadcastStream.
    I tried to send message by call defaultBinaryMessenger.send which is wrong.
    Actually, defaultBinaryMessenger.handlePlatformMessage is right.

  4. github-actions commented on Aug 28, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  5. locked as resolved and limited conversation to collaborators on Aug 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions