最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dart - Testing a view in flutter - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

Since 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 or mocktail to mock NavigationService, DialogService, and UseCases.

  • 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);
  });
}
发布评论

评论列表(0)

  1. 暂无评论