Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

flutter - How mock a instance of File in Dart

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...