1

I want to do some unit tests on some functions, but I need to execute all the tests after the completion of a Future.

To develop my problem, Here an example of what I want to do :

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

I have tried with a alternate solution, but this does not change anything :

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

And I would have, if possible the tests organize as that, as I want a description for all the tests.

The stacktrace is something like that :

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)
4

1 回答 1

2

我猜不支持像您在此处所做的那样将测试包装在其他功能中

registerToServer(contentOfRequest).then((id){

test('测试函数1', () {

你可以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或类似的工具。

于 2015-03-17T16:03:40.933 回答