Flutter图像拾取器集成测试

avwztpqn  于 2023-04-22  发布在  Flutter
关注(0)|答案(2)|浏览(129)

我正在尝试从这个链接How to test ImagePicker in Flutter Driver?为image_picker的e2 e测试的解决方案

void main() {
      enableFlutterDriverExtension();
    
      const MethodChannel channel =
          MethodChannel('plugins.flutter.io/image_picker');
    
      setUp(() {
        channel.setMockMethodCallHandler((MethodCall methodCall) async {
          ByteData data = await rootBundle.load('images/sample.png');
          Uint8List bytes = data.buffer.asUint8List();
          Directory tempDir = await getTemporaryDirectory();
          File file = await File(
            '${tempDir.path}/tmp.tmp',
          ).writeAsBytes(bytes);
          print(file.path);
          return file.path;
        });
      });
      app.main();
    }

我的主要文件测试是完全相同的.问题是,当我使用enableFlutterDriverExtension();的测试后,开始,完成所有测试通过的信息,没有模拟器上的模拟步骤,并打印所有信息到控制台后,每一步.在控制台我得到

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: 'package:flutter_driver/src/extension/extension.dart': Failed assertion: line 222 pos 10:
'WidgetsBinding.instance == null': is not true.
#0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:47:61)
#1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
#2      enableFlutterDriverExtension (package:flutter_driver/src/extension/extension.dart:222:10)
#3      main

String getString();当我尝试调用image_picker时,测试失败。出现错误

flutter:   'package:flutter_test/src/binding.dart': Failed assertion: line 775 pos 14: '_pendingExceptionDetails != null': A test overrode FlutterError.onError
but either failed to return it to its original state, or had unexpected additional errors that it could not handle. Typically, this is caused by using expect()
before restoring FlutterError.onError.
flutter:   dart:core-patch/errors_patch.dart 47:61       _AssertionError._doThrowNew

EnableFlutterDriverExtension();do?在不测试image_picker和这个enableFlutterDriverExtension()的情况下,测试工作正常。还有其他测试image_picker的解决方案吗?

kyxcudwk

kyxcudwk1#

问题How to test ImagePicker in Flutter Driver?是超过2岁,所以它不是更实际。
在你测试它之前,你需要为一个平台实现它。你为Android实现它,就像这样:从版本0.8.1开始,Android实现支持在Android 4.3或更高版本上选择(多个)图像。
不需要配置-插件应该开箱即用。然而,强烈建议在Android内存不足时准备杀死应用程序。如何准备在Android上处理MainActivity销毁一节中讨论。
不再需要将android:requestLegacyExternalStorage=“true”作为属性添加到AndroidManifest.xml中的标记中,因为image_picker已更新为使用范围存储。
注意:使用相机拾取的图像和视频会保存到应用程序的本地缓存中,因此应该只是暂时存在。如果您需要永久存储拾取的图像,则您有责任将其移动到更永久的位置。
然后你可以通过检查它是否工作来测试它:

// it pick a image from gallery but you can change that to camera
final XFile? image = await _picker.pickImage(source: ImageSource.gallery);

你需要知道它给你一个XFile,而不是一个普通的文件,但你可以把它转换成这样的文件:

final xFile = await ImagePicker().pickImage(source: ImageSource.gallery);

final String? path = xFile!.path;

//that's the File
final bytes = await File(path!).readAsBytes();
gwbalxhn

gwbalxhn2#

为了让它在android模拟器上工作,我在我的widget测试中这样做了:

const MethodChannel channel =
    MethodChannel('plugins.flutter.io/image_picker_android');
handler(MethodCall methodCall) async {
  ByteData data = await rootBundle.load(imagePath);
  Uint8List bytes = data.buffer.asUint8List();
  Directory tempDir = await getTemporaryDirectory();
  File file = await File(
    '${tempDir.path}/tmp.tmp',
  ).writeAsBytes(bytes);
  return file.path;
}

TestWidgetsFlutterBinding.ensureInitialized();
TestDefaultBinaryMessengerBinding.instance?.defaultBinaryMessenger
    .setMockMethodCallHandler(channel, handler);

相关问题