[英]Flutter test - verifying assets exist
I have a flutter app which contains a large list of quotes, each with an associated audio file.我有一个 flutter 应用程序,其中包含大量引用,每个引用都有一个关联的音频文件。
I've written a simple test that verifies all the specified audio files are where they're supposed to be, in case of typos etc:我编写了一个简单的测试来验证所有指定的音频文件是否在它们应该在的位置,以防出现拼写错误等:
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');
});
});
This passes fine in IntelliJ, however running from the command line using flutter test
it fails on the first thing it looks for.这在 IntelliJ 中通过得很好,但是从命令行使用
flutter test
它在它寻找的第一件事上失败了。
Is there a way of doing this which will work regardless of how it's run?有没有办法做到这一点,无论它如何运行都可以工作? Why does it pass one way but not the other?
为什么它通过一种方式而不是另一种方式?
Ok so I got to the bottom of this, and it is something you can do in a unit test.好的,我终于明白了,这是你可以在单元测试中做的事情。
To diagnose, I added the line print(Directory.current);
为了诊断,我添加了一行
print(Directory.current);
to the test.到测试。 Running in IntelliJ, I get
/home/project_name
.在 IntelliJ 中运行,我得到
/home/project_name
。 From the command line, it's /home/project_name/test
.从命令行,它是
/home/project_name/test
。 So just a simple file path thing to resolve.所以只是一个简单的文件路径问题来解决。
Edited to include Ovidiu's simpler logic for getting the right asset path编辑以包含 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');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.