Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k 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 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...