【问题标题】:Flutter test resources in codemagicCodemagic 中的 Flutter 测试资源
【发布时间】:2019-08-06 16:59:09
【问题描述】:

我想在 Flutter 测试中使用 JSON 文件来模拟 HTTP 响应。在 gitlab CI 上,测试通过了。局部也。但是在codemagic.io上出现错误:

FileSystemException: Cannot open file, path = 'test_resources/mock_response.json' (OS Error: No such file or directory, errno = 2)

【问题讨论】:

    标签: testing flutter continuous-integration codemagic


    【解决方案1】:

    这与`flutter test` sets current directory differently depending on how test was executed 有关。 apaatsio in the first comment 提供的解决方案对我有用。

    所以例如你可以有这样的东西:

    import 'dart:io';
    import 'package:path/path.dart';
    
    String loadResource(String name) => File("$_testDirectory/test_resources/$name").readAsStringSync();
    
    // From https://github.com/flutter/flutter/issues/20907#issuecomment-466185328
    final _testDirectory = join(
      Directory.current.path,
      Directory.current.path.endsWith('test') ? '' : 'test',
    );
    

    然后使用它:

    final mockResponse = loadResource('mock_response.json');
    

    【讨论】:

      猜你喜欢
      • 2020-02-23
      • 2020-01-10
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 2021-10-30
      相关资源
      最近更新 更多