marcglasberg / back_button_interceptor

Flutter Package: May be used to intercept the Android back-button, as an alternative to `WillPopScope`.
BSD 2-Clause "Simplified" License
105 stars 21 forks source link

Trouble running multiples tests that use the BackButtonInterceptor #6

Closed rodrigobastosv closed 3 years ago

rodrigobastosv commented 3 years ago

While running multiple tests that use the interceptor i got this error:

The BackButtonInterceptor threw an ERROR: 'package:provider/src/provider.dart': Failed assertion: line 189 pos 12: 'context != null': is not true..
Pending timers:
Timer (duration: 0:00:00.000000, periodic: false), created:
#0      new _FakeTimer._ (package:quiver/testing/src/async/fake_async.dart:277:42)
#6      BackButtonInterceptor.errorProcessing.<anonymous closure> (package:back_button_interceptor/src/back_button_interceptor.dart:22:12)
#7      BackButtonInterceptor.popRoute (package:back_button_interceptor/src/back_button_interceptor.dart:138:24)
#8      main.<anonymous closure>.<anonymous closure> (file:///home/rodrigo/Documentos/projects/polis/test/page/user_following_politics_page_test.dart:60:35)
#21     AutomatedTestWidgetsFlutterBinding.runTest.<anonymous closure> (package:flutter_test/src/binding.dart:1099:17)
#22     AutomatedTestWidgetsFlutterBinding.runTest.<anonymous closure> (package:flutter_test/src/binding.dart:1087:35)
(elided 32 frames from class _FakeAsync, dart:async, and package:stack_trace)

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown running a test:
A Timer is still pending even after the widget tree was disposed.
'package:flutter_test/src/binding.dart':
Failed assertion: line 1119 pos 12: '() {
      if (   _currentFakeAsync.periodicTimerCount == 0
          && _currentFakeAsync.nonPeriodicTimerCount == 0) {
        return true;
      }

      debugPrint('Pending timers:');
      for (final String timerInfo in _currentFakeAsync.pendingTimersDebugInfo) {
        final int firstLineEnd = timerInfo.indexOf('\n');
        assert(firstLineEnd != -1);

        // No need to include the newline.
        final String firstLine = timerInfo.substring(0, firstLineEnd);
        final String stackTrace = timerInfo.substring(firstLineEnd + 1);

        debugPrint(firstLine);
        debugPrintStack(stackTrace: StackTrace.fromString(stackTrace));
        debugPrint('');
      }
      return false;
    }()'

When the exception was thrown, this was the stack:
#2      AutomatedTestWidgetsFlutterBinding._verifyInvariants (package:flutter_test/src/binding.dart:1119:12)
#3      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:716:7)
<asynchronous suspension>
#6      TestWidgetsFlutterBinding._runTest (package:flutter_test/src/binding.dart:683:14)
#7      AutomatedTestWidgetsFlutterBinding.runTest.<anonymous closure> (package:flutter_test/src/binding.dart:1083:24)
#13     AutomatedTestWidgetsFlutterBinding.runTest (package:flutter_test/src/binding.dart:1080:15)
#14     testWidgets.<anonymous closure> (package:flutter_test/src/widget_tester.dart:133:24)
#15     Declarer.test.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart:172:27)
<asynchronous suspension>
#16     Declarer.test.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart)
#17     Invoker.waitForOutstandingCallbacks.<anonymous closure> (package:test_api/src/backend/invoker.dart:246:15)
#22     Invoker.waitForOutstandingCallbacks (package:test_api/src/backend/invoker.dart:243:5)
#23     Declarer.test.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart:170:33)
#28     Declarer.test.<anonymous closure> (package:test_api/src/backend/declarer.dart:169:13)
#29     Invoker._onRun.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/invoker.dart:400:30)
(elided 33 frames from class _AssertionError, class _FakeAsync, class _RawReceivePortImpl, class _Timer, dart:async, dart:async-patch, and package:stack_trace)
rodrigobastosv commented 3 years ago

As a solution i ended up putting BackButtonInterceptor.removeAll(); on the setUp method of my widgets test. It completed solved this issue.

rodrigobastosv commented 3 years ago

@marcglasberg if you feel like this info is important i created a PR to add this on the README