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.
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.
Share Improve this question asked Mar 28 at 19:38 PumbaPumba 865 bronze badges1 Answer
Reset to default 0Since your views are basically widgets, i will recommend you write widget tests for your views. You should:
Mock all external dependencies (e.g.,
GetIt
services).Use
Mockito
ormocktail
to mockNavigationService
,DialogService
, andUseCases
.Avoid real API calls or platform dependencies.
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);
});
}