飞镖测试代码:
_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
哪里错了?
答案 0 :(得分:2)
测试在执行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
之前,测试不会结束。