jeudi 2 janvier 2020

WidgetTest navigation in flutter with mockito not working

Super new to flutter testing. I don't understand why I get the following error on

verify(mockNavigationObserver.didPush(any, any));

════════════════════════════════════════════════════════════════════════════════════════════════╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
  No matching calls (actually, no calls at all).
(If you called `verify(...).called(0);`, please instead use `verifyNever(...);`.)

When the exception was thrown, this was the stack:
#0      fail (package:test_api/src/frontend/expect.dart:153:30)
#1      _VerifyCall._checkWith (package:mockito/src/mock.dart:648:7)
#2      _makeVerify.<anonymous closure> (package:mockito/src/mock.dart:935:18)
#3      main.<anonymous closure>.<anonymous closure> (file:<my_project_path>/test/navigation_test.dart:53:13)
<asynchronous suspension>
#4      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:124:25)
#5      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:696:19)
<asynchronous suspension>
#8      TestWidgetsFlutterBinding._runTest (package:flutter_test/src/binding.dart:679:14)
#9      AutomatedTestWidgetsFlutterBinding.runTest.<anonymous closure> (package:flutter_test/src/binding.dart:1050:24)
#15     AutomatedTestWidgetsFlutterBinding.runTest (package:flutter_test/src/binding.dart:1047:15)
#16     testWidgets.<anonymous closure> (package:flutter_test/src/widget_tester.dart:121:22)
#17     Declarer.test.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart:171:27)
<asynchronous suspension>
#18     Invoker.waitForOutstandingCallbacks.<anonymous closure> (package:test_api/src/backend/invoker.dart:242:15)
#23     Invoker.waitForOutstandingCallbacks (package:test_api/src/backend/invoker.dart:239:5)
#24     Declarer.test.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart:169:33)
#29     Declarer.test.<anonymous closure> (package:test_api/src/backend/declarer.dart:168:13)
#30     Invoker._onRun.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/invoker.dart:392:25)
#44     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
#45     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
#46     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
(elided 28 frames from class _FakeAsync, package dart:async, package dart:async-patch, and package stack_trace)

The test description was:
  StartPage .. LogInButton -> LogIn Screen```

my navigation_test.dart below:

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:project/auth/provider/auth_provider.dart';
import 'package:project/auth/screens/start_screen.dart';
import 'package:project/common/config/finders.dart';
import 'package:project/common/config/key_strings.dart';
import 'package:project/common/config/route_config.dart';
import 'package:provider/provider.dart';

// Mock class
class MockNavigatorObserver extends Mock implements NavigatorObserver {}

class MockAuthProvider extends Mock implements AuthProvider {}

void main() {
  group('StartPage navigation tests', () {
    NavigatorObserver mockNavigationObserver;
    AuthProvider mockAuthProvider;

    setUp(() {
      mockNavigationObserver = MockNavigatorObserver();
      mockAuthProvider = MockAuthProvider();
    });

    Widget createWidgetForTesting({Widget child}) {
      return MaterialApp(
        home: child,
        routes: RouteConfig.routes,
      );
    }

    Future<Null> _buildStartScreen(WidgetTester tester) async {
      await tester.pumpWidget(
          ChangeNotifierProvider<AuthProvider>.value(
        value: mockAuthProvider,
        child: createWidgetForTesting(child: StartScreen()),
      ));

      verify(mockNavigationObserver.didPush(any, any));
    }

    Future<Null> _navigateToLogInPage(WidgetTester tester) async {
      await tester.tap(find.byKey(Key(START_SCREEN_LOGIN_BUTTON_KEY)));
      await tester.pumpAndSettle();
    }

    testWidgets('StartPage .. LogInButton -> LogIn Screen',
        (WidgetTester tester) async {
      await _buildStartScreen(tester);
      await _navigateToLogInPage(tester);

      verify(mockNavigationObserver.didPush(any, any));

      expect(createProfileScreen, findsOneWidget);
      expect(createProfileScreenPurposeStatement, findsOneWidget);
      expect(familyNameFormField, findsOneWidget);
      expect(phoneNumber, findsOneWidget);
      expect(homeAddressFormField, findsOneWidget);
      expect(familyNoteFormField, findsOneWidget);
      expect(familyNameAsterisk, findsOneWidget);
    });
  });
}

Aucun commentaire:

Enregistrer un commentaire