flutter - 在 Flutter 中运行单元测试时出现 MissingPluginException
问题描述
我正在使用Google ML Kit Plugin从给定的图像中扫描条形码和二维码,我尝试为 Google ML Kit Plugin 提供的 processImage 方法编写单元测试。在运行我的测试代码时,我遇到了这个错误。我附上了我的单元测试代码和应用程序代码。
错误:
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('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);
});
我的代码:
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();
}
}
解决方案
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);
});
推荐阅读
- r - 如何在 R 编程语言中的 x 轴 ggplot 上显示日期?
- python - hash_table 初始化,发生了什么?
- javascript - 如何使用JS识别当前值
- windows - ipython 控制台中的错误“异常 [WinError 995] I/O 操作已中止”
- php - 已解决 - Symfony5 - URI“/sitemap.xml”的控制器不可调用
- python - Python pandas,对数据在时间上的均匀分布进行评分
- xamarin - 如何在 Xamarin Forms 中实现/注册单例 HttClientFactory
- google-sheets - Google 表格数组公式运行余额按类别列分层并按日期列排序
- c# - 查找重复项并将其从列表中删除的 Linq 查询
- mysql - 如何根据另一张表的数据更新一张表?