How can I make test inside a then statement in Dart

Issue

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)

Solution

I guess it is not supported to wrap tests in other functions like you did here

registerToServer(contentOfRequest).then((id){

test(‘test function1’, () {

You can use setUp() to prepare for the test

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

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

The disadvantage is that currently there is no way to do setup for a group of tests. setUp() and tearDown() is called before/after each single test.
Please add a comment (+1 or similar) to https://github.com/dart-lang/unittest/issues/18

When using async calls within setUp/tearDown/test always return the future. The test framework recognizes when a future is returned and waits until the future finishes before it ends. This way you don’t have to cope with expectAsync or similar tools.

Answered By – Günter Zöchbauer

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.