为什么异步测试通过,但也有一些显示错误信息?(Why the async test passed,

2019-10-19 06:24发布

飞镖测试代码:

_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));
    })));
});

当我的IntelliJ-IDEA运行它称为“单元测试”,它通过了,但显示出一些错误信息:

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

哪里错了?

Answer 1:

内部的代码之前测试结束new Timer()被执行。

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();
      });
    });
  });
}

这样的测试没有结束,直到callback被调用。



文章来源: Why the async test passed, but there are some error messages displayed?