我真的不明白protectAsyncN 方法的目的。
有人可以解释一下它应该如何工作吗?例如,想象以下测试用例:
import "package:unittest/unittest.dart";
import "dart:async";
void main() {
test("Protect async", () {
// given
var controller = new StreamController();
// when
controller.add("This is correct!");
// then
controller.stream.listen(protectAsync1((event) {
expect(event, equals("This is correct!"));
}));
});
}
行为应该是什么?我希望这个测试用例能够通过,但是我收到以下消息:
unittest-suite-wait-for-done
ERROR: Protect async
Callback called (1) after test case Protect async has already been marked as pass.
0 PASSED, 0 FAILED, 1 ERRORS
Unhandled exception:
Exception: Some tests failed.
#0 SimpleConfiguration.onDone (package:unittest/src/simple_configuration.dart:213:9)
#1 _completeTests (package:unittest/unittest.dart:779:17)
#2 _runTest (package:unittest/unittest.dart:734:19)
#3 _nextTestCase (package:unittest/unittest.dart:641:11)
#4 _asyncRunCallback (dart:async/schedule_microtask.dart:18)
#5 _asyncRunCallback (dart:async/schedule_microtask.dart:21)
#6 _createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:11)
#7 _Timer._createTimerHandler._handleTimeout (timer_impl.dart:151)
#8 _Timer._createTimerHandler._handleTimeout (timer_impl.dart:159)
#9 _Timer._createTimerHandler._handleTimeout (timer_impl.dart:159)
#10 _Timer._createTimerHandler.<anonymous closure> (timer_impl.dart:166)
#11 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:93)
我尝试过使用guardAsync,它按预期工作。但是,我真的不明白protectAsync。
有人可以解释一下它的用途和正确用法吗?非常感谢!