How to wait for an asynchronous setup in a unit test, in Dart?

一笑奈何 提交于 2019-12-04 19:33:45

问题


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.



来源:https://stackoverflow.com/questions/14994518/how-to-wait-for-an-asynchronous-setup-in-a-unit-test-in-dart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!