如何在 Dart 中的 then 语句中进行测试

How can I make test inside a then statement in Dart

我想对一些功能做一些单元测试,但是我需要在一个Future完成后执行所有的测试。

为了解决我的问题,这里有一个我想做的例子:

registerToServer(contentOfRequest).then((id){
  test('test function1', () {
    function1(id, contentOfRequest).then(expectAsync((val){
       expect(val, whatIExpect);
    }));
  });
  test('test function2', () {
    function2(id, contentOfRequest).then(expectAsync((val){
       expect(val, whatIExpect);
    }));
  }):
  ...
};

我已经尝试过替代解决方案,但这并没有改变任何东西:

String id;
registerToServer(contentOfRequest).then((sid){
  id = sid;
}).then((_){
  test('test function1', () {
    function1(id, contentOfRequest).then(expectAsync((val){
       expect(val, whatIExpect);
    }));
  });
  test('test function2', () {
    function2(id, contentOfRequest).then(expectAsync((val){
       expect(val, whatIExpect);
    }));
  }):
  ...
};

如果可能的话,我会这样组织测试,因为我想要所有测试的描述。

堆栈跟踪是这样的:

Unhandled exception:
Uncaught Error: Bad state: Not allowed when tests are running.
Stack Trace:
#0      _requireNotRunning (package:unittest/unittest.dart:430:3)
#1      test (package:unittest/unittest.dart:100:21)
#2      main.<anonymous closure> (file:///.../server_test.dart:260:11)
#3      _RootZone.runUnary (dart:async/zone.dart:1155)
#4      _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:484)
#5      _Future._propagateToListeners (dart:async/future_impl.dart:567)
#6      _Future._completeWithValue (dart:async/future_impl.dart:358)
#7      _Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:412)
#8      _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#9      _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#10     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:84)
#11     _startIsolate (dart:isolate-patch/isolate_patch.dart:244)
#12     _startMainIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:192)
#13     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:130)

#0      _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:886)
#1      _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#2      _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#3      _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:84)
#4      _startIsolate (dart:isolate-patch/isolate_patch.dart:244)
#5      _startMainIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:192)
#6      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:130)

我想不支持像您在此处所做的那样在其他函数中包装测试

registerToServer(contentOfRequest).then((id){

测试('test function1', () {

可以用setUp()备考

main() {
  group('xxx', () {
    setUp(() {
      return registerToServer(contentOfRequest);
    });

    test('some', () {
      return function1(id, contentOfRequest).then((val){
       expect(val, whatIExpect);
      }));
    });
  });
}

缺点是目前没有办法为一组测试做设置。 setUp()tearDown() 被称为 before/after 每个单独的测试。 请在 https://github.com/dart-lang/unittest/issues/18

中添加评论(+1 或类似内容)

在 setUp/tearDown/test 内使用异步调用时总是 return 未来。测试框架识别未来何时被 returned 并等待直到未来结束。这样您就不必应付 expectAsync 或类似的工具。