颤振测试-验证资产是否存在

时间:2020-02-25 13:51:26

标签: testing flutter assets

我有一个Flutter应用,其中包含大量的引号,每个引号都有一个关联的音频文件。

我编写了一个简单的测试,以验证所有指定的音频文件在输入错误等情况下的正确位置:

test('specified audio file should exist for all quotes', () {
    ALL_QUOTES.forEach((quote) {
      final expectedPath = 'assets/${quote.filename}.wav';
      final exists = new File(expectedPath).existsSync();
      expect(exists, isTrue, reason: '$expectedPath does not exist');
    });
  });

这在IntelliJ中可以正常通过,但是使用flutter test从命令行运行时,它在查找的第一件事上失败。

有没有一种方法可以不管运行如何而起作用?为什么它会通过一种方法,而不会通过另一种方法?

1 个答案:

答案 0 :(得分:0)

好,所以我深入到此,它 是您可以在单元测试中完成的事情。

为进行诊断,我在测试中添加了行print(Directory.current);。在IntelliJ中运行,我得到/home/project_name。在命令行中,它是/home/project_name/test。因此,只需解决一个简单的文件路径即可。

经过编辑,以包含Ovidiu的更简单逻辑来获取正确的资产路径

void main() {
  test('specified audio file should exist for all quotes', () {
    ALL_QUOTES.forEach((quote) {
      final expectedPath = 'assets/${quote.filename}.wav';
      final exists = _getProjectFile(expectedPath).existsSync();
      expect(exists, isTrue, reason: '$expectedPath does not exist');
    });
  });
}

File _getProjectFile(String path) {
  final String assetFolderPath = Platform.environment['UNIT_TEST_ASSETS'];
  return File('$assetFolderPath/$path');
}