How can I “sleep” a Dart program

前端 未结 7 2236
梦谈多话
梦谈多话 2020-12-12 23:39

I like to simulate an asynchronous web service call in my Dart application for testing. To simulate the randomness of these mock calls responding (possibly out of order) I\

7条回答
  •  别那么骄傲
    2020-12-12 23:47

    This a useful mock that can take an optional parameter to mock an error:

      Future _mockService([dynamic error]) {
        return new Future.delayed(const Duration(seconds: 2), () {
          if (error != null) {
            throw error;
          }
        });
      }
    

    You can use it like this:

      await _mockService(new Exception('network error'));
    

提交回复
热议问题