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

时间:2015-03-17 15:03:15

标签: unit-testing dart future dart-async dart-unittest

我想对某些函数进行一些单元测试,但是我需要在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)

1 个答案:

答案 0 :(得分:1)

我想不支持在其他函数中包装测试,就像你在这里做的那样

registerToServer(contentOfRequest).then((id){

测试(&#39;测试功能1&#39;,(){

您可以使用setUp()准备考试

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

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

缺点是目前无法为一组测试进行设置。在每次单次测试之前/之后调用setUp()tearDown()。 请在https://github.com/dart-lang/unittest/issues/18

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

在setUp / tearDown / test中使用异步调用时,始终返回未来。测试框架识别何时返回未来并等到将来结束之前完成。这样您就不必处理expectAsync或类似工具。