Why the async test passed, but there are some error messages displayed?

Issue

Dart test code:

_doSomething2(callback(int x, int y)) {
    callback(1, 2);
}

test('async test, check a function with 2 parameters', () {
    new Timer(new Duration(milliseconds:100), _doSomething2(expectAsync2((x, y) {
        expect(x, equals(1));
        expect(y, equals(2));
    })));
});

When I run it in Intellij-IDEA as “unittest”, it passed, but there is some error message shown:

Testing started at PM11:08 ...
Unhandled exception:
The null object does not have a method 'call'.

NoSuchMethodError : method not found: 'call'
Receiver: null
Arguments: []
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
#1      _createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:11)
#2      _handleTimeout (timer_impl.dart:283)
#3      _handleTimeout (timer_impl.dart:292)
#4      _handleTimeout (timer_impl.dart:292)
#5      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:115)

Process finished with exit code 255

Where is wrong?

Solution

The test ends before the code inside the new Timer() is executed.

void main(List<String> args) {
  test('async test, check a function with 2 parameters', () {
      var callback = expectAsync0(() {});
      new Timer(new Duration(milliseconds:100), () {
          _doSomething2((x, y) {
          expect(x, equals(1));
          expect(y, equals(2));
          callback();
      });
    });
  });
}

This way the test doesn’t end until callback is called.

Answered By – Günter Zöchbauer

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.