dart - 为什么异步测试通过了,但是显示了一些错误消息?

标签 dart dart-unittest

Dart测试代码:

_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中将其作为“unittest”运行时,它通过了,但是显示了一些错误消息:
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

哪里错了?

最佳答案

测试在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,测试才结束。

关于dart - 为什么异步测试通过了,但是显示了一些错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21409349/

相关文章:

Dart - 如何针对调用 exit() 的函数编写测试?

flutter - 如何从 Flutter 桌面应用程序中打开 CMD

dart - 运行测试时加载 dartium 失败

flutter - 当我设置超时持续时间时,没有互联网连接的未处理的套接字异常

flutter - 在Flutter网站中上传图片

dart - 测试包中是否有正则表达式匹配器

dart - 有没有办法在 Dart 语言中获得单元测试的测试覆盖率报告?

Dart事件队列: How to debug event queue?

Flutter:热重载卡在 Visual Studio Code 中

flutter - 如何使用SizedBox在其自己的类中包装派生的RaisedButton对象?