How to mock VideoPlayerController.network() in Flutter

Viewed 373

I'm writing a simple test for my video player widget that uses the video_player plugin. I'm not able to mock the network request made by the video controller for fetching the video over network. My widget code looks like:

late VideoPlayerController _videoController;

@override
void initState() {
    _videoController = VideoPlayerController.network(widget.videoUrl);
// rest of the code
}

and test code:

VideoPlayerController _videoController =
        VideoPlayerController.network(videoUrl);

    when(VideoPlayerController.network(videoUrl))
        .thenAnswer((_) => _videoController);

This is not working because it is not able to stub the network request method correctly. Any ideas for mocking it correctly? I have several other tests in my code where I have mocked my api client class which makes network requests, but this one is a little different. I'm using mockito for mocking.

Please help!

0 Answers
Related