flutterdarttestingmobile

Testing a view in flutter


Is it ideal to test a view in flutter or is it better to test only the widget since view are generally dependant on a lot of stuff in your project?

I trying to test a login view in flutter to improve my code coverage and I'm realising the view itself is dependant on a lot of stuff registered inside GetIt. There is the NavigationService, the DialogService and some Usecase.


Solution

  • Since your views are basically widgets, i will recommend you write widget tests for your views. You should:

    A sample on what such test could look like is shown below. Its a hypothetical test for a LoginView, that uses a NavigationService, DialogService and LoginUsecase.

    Typically, you mock these dependencies, register them in getIt(since that is what you said you are using), then stub the calls made to these dependencies.

    One would expect that you use Mockito or Mocktail to mock these dependencies.

    class MockNavigationService extends Mock implements NavigationService {}
    class MockDialogService extends Mock implements DialogService {}
    class MockLoginUsecase extends Mock implements LoginUsecase {}
    
    void main() {
      late MockNavigationService mockNavigationService;
      late MockDialogService mockDialogService;
      late MockLoginUsecase mockLoginUsecase;
    
      setUp(() {
        mockNavigationService = MockNavigationService();
        mockDialogService = MockDialogService();
        mockLoginUsecase = MockLoginUsecase();
    
        // Register mocks in GetIt
        GetIt.I.registerSingleton<NavigationService>(mockNavigationService);
        GetIt.I.registerSingleton<DialogService>(mockDialogService);
        GetIt.I.registerSingleton<LoginUsecase>(mockLoginUsecase);
      });
    
      tearDown(() {
        GetIt.I.unregister<NavigationService>();
        GetIt.I.unregister<DialogService>();
        GetIt.I.unregister<LoginUsecase>();
      });
    
      testWidgets('LoginView shows error dialog when login fails', (tester) async {
        // Arrange:
        when(mockLoginUsecase(any)).thenThrow(Failure('Invalid credentials'));
        when(mockDialogService.showError(any)).thenAnswer((_) async => true);
    
        // Act:
        await tester.pumpWidget(MaterialApp(home: LoginView()));
        await tester.tap(find.byKey(Key('loginButton')));
        await tester.pump();
    
        // Assert:
        verify(mockDialogService.showError('Invalid credentials')).called(1);
      });
    }