flutter - PlatformException,尝试模拟 file_picker 进行集成测试后参数无效,Flutter

标签 flutter mocking integration-testing filepicker.io flutter-integration-test

我已经使用file_picker在我的应用程序中,现在我创建一个集成测试。 我一直在寻找一种模拟 file_picker 或依赖项的方法,这就是结果:

import 'dart:io';
import 'dart:typed_data';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:path_provider/path_provider.dart';

void main() async {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  setUp(() {
    mockFilePicker();
  });

  testWidgets("test", (WidgetTester tester) async {
    // await app.main();
    // .. test case ..
  });
}

mockFilePicker() {
  const MethodChannel channel =
      MethodChannel('miguelruivo.flutter.plugins.filepicker');
  channel.setMockMethodCallHandler((MethodCall methodCall) async {
    print("MockMethodChannel run");

    ByteData data = await rootBundle.load('assets/images/ic_bill.png');
    Uint8List bytes = data.buffer.asUint8List();
    Directory tempDir = await getTemporaryDirectory();
    File file = await File(
      '${tempDir.path}/image.png',
    ).writeAsBytes(bytes);

    PlatformFile platformFile = PlatformFile(
        name: "image.png", size: file.lengthSync(), path: file.path);
    FilePickerResult filePickerResult = FilePickerResult([platformFile]);
    return filePickerResult;
  });
}

使用该代码,我收到如下错误:

[MethodChannelFilePicker] Platform exception: PlatformException(error, Invalid argument: Instance of 'FilePickerResult', null, null)

如何解决这个问题?

最佳答案

看起来 channel 方法调用需要一个 map 列表而不是 FilePickerResult 对象。 map 格式为:

{
  'name': String,
  'path': String,
  'bytes': Uint8List,
  'size': int,
}

以下模拟 channel 对我有用:

  const MethodChannel channelFilePicker =
      MethodChannel('miguelruivo.flutter.plugins.filepicker');

  TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
      .setMockMethodCallHandler(channelFilePicker,
          (MethodCall methodCall) async {
    final ByteData data = await rootBundle.load('assets/icon/Icon.png');
    final Uint8List bytes = data.buffer.asUint8List();
    final Directory tempDir = await getTemporaryDirectory();
    final File file = await File(
      '${tempDir.path}/tmp.tmp',
    ).writeAsBytes(bytes);
    print(file.path);
    return [
      {
        'name': "Icon.png",
        'path': file.path,
        'bytes': bytes,
        'size': bytes.lengthInBytes,
      }
    ];
  });

关于flutter - PlatformException,尝试模拟 file_picker 进行集成测试后参数无效,Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69030659/

相关文章:

flutter - 调试 Flutter 应用程序的方法有哪些?

unit-testing - RhinoMocks : Clear or reset AssertWasCalled()

flutter - 在 flutter 中将 List.toString() 转换为 List

android - Flutter - 任务 ':connectivity:compileDebugJavaWithJavac' 执行失败

android - 如何在Flutter中做隐式 Intent ?

python - 忽略一次测试的类级别补丁装饰器

c# - FakeItEasy 是否支持部分模拟的想法?

java - Arquillian 测试中的 Persistence.xml 被覆盖

unit-testing - 集成测试是一个总称吗?如果是,它包括哪些类型的测试?

grails - 如何在 IntelliJ IDEA 中进行 Grails 3 集成测试