1

I have a flutter package I am working on, and if I build it for Windows it builds and from the output I can see it's working fine. However, when I try to run the tests as tests (as opposed to building it) I always get the MissingPluginException(No implementation found for method) error. It's as though the test isn't using something that is being used by actually building the app.

Is there a fix for this? I've called TestWidgetsFlutterBinding.ensureInitialized() before running tests, but always get the same error....

Test Code (works when built):

 void main() {
  TestWidgetsFlutterBinding.ensureInitialized();
  final networkHealth = IwNetworkHealth();

  test('returns ip address', () async {
      print("WifiIP: ${await networkHealth.getWifiIp()}");
  });
}

Actual called code:

  Future<String?> getWifiIp() async{
    var info = NetworkInfo();

    var wifiIP = await info.getWifiIP();

    return wifiIP;
  }
  • Welcome to SO! Would you please provide a [minimal-reproducible-example](https://stackoverflow.com/help/minimal-reproducible-example) so others are able to answer your question a bit more easily. – lepsch Jun 21 '22 at 17:32
  • I have now added example code. – TheDizzyEgg Jun 21 '22 at 17:41
  • [Does this answer your question](https://stackoverflow.com/questions/64118542/missingpluginexceptionno-implementation-found-for-method-firebaseinitializecor)? The thing is that Flutter tests only the UI. It doesn't run any native code like multi-platform plugins. To solve your problem you need to mock `NetworkInfo` ([Mockito would help](https://pub.dev/packages/mockito)). – lepsch Jun 21 '22 at 19:08

1 Answers1

1

Thankyou @lepsch for your direction in the comments, it does indeed seem that any multi-platform plugins need to be mocked. I am now able to run tests by adding my own handlers to intercept the channel methods, which allows me to test my code properly. Here is an example of the test setup function that I'm now using:

setUpAll(() {
    const networkInfoChannel = MethodChannel('dev.fluttercommunity.plus/network_info');

    networkInfoHandler(MethodCall methodCall) async {
      if (methodCall.method == 'wifiIPAddress') {
        return Future<String?>.delayed(const Duration(milliseconds: 100)).then((value) => "192.168.0.20");
      }

      return "unknown";
    }

    TestWidgetsFlutterBinding.ensureInitialized();

    TestDefaultBinaryMessengerBinding.instance?.defaultBinaryMessenger
        .setMockMethodCallHandler(networkInfoChannel, networkInfoHandler);
  });