2

I'm using Google ML Kit Plugin for scanning barcode and qr code from given Image, I tried to write unit test for processImage method which is provided by the Google ML Kit Plugin. While running running my test code I'm facing this error. I attached my unit test code and application code.

Error:

C:\FlutterSDK\Flutter2.5.2\flutter\bin\flutter.bat --no-color test --machine --start- 
paused test\view\widgets\barcodescanner_test.dart
Testing started at 11:20 ...

package:flutter/src/services/platform_channel.dart 154:7  MethodChannel._invokeMethod

MissingPluginException(No implementation found for method vision#startBarcodeScanner on 
channel google_ml_kit)

Test Code:

test('barcode test',() async {
BarcodeScannerUtil barcodeScannerUtil=BarcodeScannerUtil();
List<Barcode> barcodeList=await barcodeScannerUtil.getBarcode(InputImage.fromFilePath(r'C:\FlutterProjects\KK\Project\Dev\assets\images\ac.png'));
expect(barcodeList.length, 0);
});

My Code:

class BarcodeScannerUtil {
  BarcodeScanner barcodeScanner = GoogleMlKit.vision.barcodeScanner();

  Future<List<Barcode>> getBarcode(InputImage inputImage) async {
    List<Barcode> barcodeList = await barcodeScanner.processImage(inputImage);
    return barcodeList;
  }

  void close() {
    barcodeScanner.close();
  }
}
MSARKrish
  • 3,355
  • 4
  • 30
  • 43

1 Answers1

3
test('Barcode Method Test',() async {
  const MethodChannel('google_ml_kit')
      .setMockMethodCallHandler((MethodCall methodCall) async {
    if (methodCall.method == 'vision#startBarcodeScanner') {
      return <Barcode>[];
    }
    return null;
  });
  BarcodeScannerUtil barcodeScannerUtil=BarcodeScannerUtil();
  List<Barcode> barcodeList=await barcodeScannerUtil.getBarcode(InputImage.fromFilePath(r'C:\FlutterProjects\KK\Project\Dev\assets\images\ac.png'));
  expect(barcodeList.length, 0);
});
MSARKrish
  • 3,355
  • 4
  • 30
  • 43