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

前端 未结 2 880
不思量自难忘°
不思量自难忘° 2021-02-14 19:56

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.

2条回答
  •  眼角桃花
    2021-02-14 20:42

    While the accepted answer by Seth is correct, the following example may be easier to understand and reuse. It returns a Future and performs the setup in the Future's async worker function:

    setUp(() {
      return Future(() async {
        await someFuture();
        callSomeFunction();
        await anotherFuture();
      });
    });
    

    The test cases will be called after the last call to anotherFuture() returns.

提交回复
热议问题