My unit tests require a setup that needs to run asynchronously. That is, I need to wait for the setup to finish before the tests are run, but the setup deals with Futures.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
With Dart M3, the setUp
function can optionally return a Future
. If setUp returns a Future, the unittest framework will wait for the Future to complete before running the individual test methods.
Here is an example:
group(('database') {
var db = createDb();
setUp(() {
return openDatabase()
.then((db) => populateForTests(db));
});
test('read', () {
Future future = db.read('foo');
future.then((value) {
expect(value, 'bar');
});
expect(future, completes);
});
});
Learn more about setUp.