Skip to content

How to mock EventChannel in unit test? #38954

@befovy

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!

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