I want mock the method file.writeAsBytes
, but I don't know how to make this.
note: I do not intend to save the files in the test, just overwrite the method so that I can get the list of paths.
Has anyone ever experienced this?
My code is
class ShareHelper {
static Future<List<String>> createFilesFromString(
List<FileShare> files) async =>
Future.wait(
files.map((fileShare) => _createFileFromString(fileShare)).toList());
static Future<String> _createFileFromString(FileShare fileShare) async {
final str = fileShare.base64.split(',');
Uint8List bytes = base64.decode(str[1]);
String dir = (await getTemporaryDirectory()).path;
final fileName = _createFileName(fileShare.name);
File file = File(
"$dir/" + fileName);
await file.writeAsBytes(bytes);
return file.path;
}
}
My unit test:
test('Deve compartilhar os arquivos', () async {
PathProviderPlatform.instance = MockPathProviderPlatform();
final files = [
FileShare(name: 'Teste/A', base64: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUg...'),
];
final paths = await ShareHelper.createFilesFromString(files);
expect(paths, []);
});
});
question from:
https://stackoverflow.com/questions/65830661/how-mock-a-instance-of-file-in-dart 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…