admin 管理员组文章数量: 1086019
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);
});
}
本文标签: dartTesting a view in flutterStack Overflow
版权声明:本文标题:dart - Testing a view in flutter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744016821a2519070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论