I really don't understand the purpose of protectAsyncN methods.
Could someone please explain me how should it work? For example, imagine following test case:
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!"));
}));
});
}
What should the behavior be? I would expect this test case to pass, however I get the following message:
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)
I have tried using guardAsync and it works as expected. However, I really don't understand protectAsync.
Could someone explain me the purpose and correct usage of it? Thanks a lot!