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

谁都会走 提交于 2019-12-11 09:03:05

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/21409349/why-the-async-test-passed-but-there-are-some-error-messages-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!