Flutter 测试 - 验证资产是否存在
Flutter test - verifying assets exist
我有一个 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
它在它寻找的第一件事上失败了。
有没有一种无论运行如何都有效的方法?为什么它通过一种方式而不通过另一种方式?
好的,我已经深入了解了,它是你可以在单元测试中做的事情。
为了诊断,我在测试中添加了行 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');
}
我有一个 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
它在它寻找的第一件事上失败了。
有没有一种无论运行如何都有效的方法?为什么它通过一种方式而不通过另一种方式?
好的,我已经深入了解了,它是你可以在单元测试中做的事情。
为了诊断,我在测试中添加了行 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');
}