I already use file_picker in my apps, and now I create an integration test. I've been looking for a way to mock file_picker or dependencies, and this is the result:
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;
});
}
With that code, I get an error like this:
[MethodChannelFilePicker] Platform exception: PlatformException(error, Invalid argument: Instance of 'FilePickerResult', null, null)
How to solve this?
Looks like the channel method call is expecting a list of maps instead of FilePickerResult object. The format of the map is:
The following mock channel worked for me: