question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MethodChannel to mock ResultType.noAppToOpen in test

See original GitHub issue

Hello,

Currently I am trying to mock the ResultType.noAppToOpen in the test as shown below. image

So I did the method channel below. const MethodChannel('open_file').setMockMethodCallHandler(methodCall) { return OpenResult(message: 'No APP found to open this file.' , type: ResultType.noAppToOpen);

But I am unable to have the desired return of ResultType.noAppToOpen in my test. Am I doing the methodChannel wrongly for this since there is filePath inside OpenFile.open(filePath) ? Or is the methodChannel not working for this. Thank you.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:11

github_iconTop GitHub Comments

1reaction
mixinDennisBarzanoffMixincommented, Apr 4, 2022

just make another wrapper service and mock it, that would work regardless of how this library is written. I did this in my case

1reaction
ValentinVignalcommented, Feb 18, 2022

Or and idea would be to not use static methods anymore as they are not mockable, you could do something like that:

class OpenFile {
  OpenFile._();

  static OpenFile _instance = OpenFile._();

  static OpenFile get instance => _instance;

  @visibleForTesting
  static OpenFile set instance(OpenFile instance) {
    // So we can give a mock in the tests
    _instance = instance;
  }
  
  // Not a static method anymore.
  Future<OpenResult> open(/* ... */) {/* ... */}  
}

And instead of do

OpenFile.open();

it will be

OpenFile.instance.open();
Read more comments on GitHub >

github_iconTop Results From Across the Web

dart - MethodChannel to mock OpenFile package in flutter for ...
Currently I am trying to mock the package of OpenFile in Flutter for ResultType.noAppToOpen in the test here. So I did the method...
Read more >
[integration_test] Mocking method channels with dynamic ...
We are testing our app with integrationtests. At one point we mock method channel with of a plugin. In the method channel name...
Read more >
Transition of platform channel test interfaces to flutter_test ...
The setMockMessageHandler method related APIs have moved from package:flutter to package:flutter_test.
Read more >
platform_channel_test.dart - Google Git
const MethodChannel channel = MethodChannel('ch7', jsonMethod);. test('can invoke method and get result', () async {. BinaryMessages.setMockMessageHandler(.
Read more >
channel_mock | Flutter Package - Pub.dev
A package to help mock communications channels in Flutter to make unit testing and mocking flutter plugins easier.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found