如何在单元测试中等待异步设置,在Dart? [英] How to wait for an asynchronous setup in a unit test, in Dart?

查看:226
本文介绍了如何在单元测试中等待异步设置,在Dart?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的单元测试需要一个需要异步运行的设置。也就是说,我需要等待安装程序在测试运行之前完成,但是安装程序处理Futures。

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.

推荐答案

使用Dart M3, setUp 函数可以选择性地返回 Future 。如果setUp返回一个Future,单元测试框架将等待Future在运行各个测试方法之前完成。

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.

这里是一个例子:

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);
  });
});

进一步了解 setUp

这篇关于如何在单元测试中等待异步设置,在Dart?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆