My widgets tests work separately but wont work together

Viewed 69

this is my code:

void main() {
  testWidgets("Localiza os widgets na home", (WidgetTester tester) async {
    await tester.pumpWidget(const MaterialApp(home: HomeView()));

    final Finder drawer = find.byType(IconButton);
    await tester.tap(drawer);
    expect(drawer, findsOneWidget);

    await tester.pumpAndSettle();
    final Finder listTileDrawer = find.byType(ListTile);
    expect(listTileDrawer, findsNWidgets(5));

    final Finder listViewDrawer = find.byType(ListView);
    expect(listViewDrawer, findsOneWidget);
  });

  testWidgets("Valida se snackBar está sendo exibida corretamente", (WidgetTester tester) async {
    await tester.pumpWidget(const MaterialApp(
        home: Scaffold(
            body: BotaoRedirecionamentoExterno(
      url: 'url invalida',
      texto: 'teste',
    ))));
    final botao = find.byType(BotaoRedirecionamentoExterno);
    await tester.tap(botao);
    await tester.pumpAndSettle();
    final Finder snackBar = find.byType(SnackBar);
    expect(snackBar, findsOneWidget);
  });
}

When I run: test/view/home/home_widget_test.dart --no-sound-null-safety it works as expected:

I/flutter (11181): 00:01 +1: Valida se snackBar está sendo exibida corretamente
I/flutter (11181): 00:02 +2: All tests passed!

But when I try: flutter test --no-sound-null-safety:

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
  Expected: exactly one matching node in the widget tree
  Actual: _WidgetTypeFinder:<zero widgets with type "SnackBar" (ignoring offstage widgets)>
   Which: means none were found but one was expected

When the exception was thrown, this was the stack:
#4      main.<anonymous closure> (file:///Users/macos/Desktop/portal-agro-sp-master/test/view/home/home_widget_test.dart:33:5)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)

This was caught by the test expectation on the following line:
  file:///Users/macos/Desktop/portal-agro-sp-master/test/view/home/home_widget_test.dart line 33
The test description was:
  Valida se snackBar está sendo exibida corretamente
════════════════════════════════════════════════════════════════════════════════════════════════════
00:26 +25 -1: /Users/macos/Desktop/portal-agro-sp-master/test/view/home/home_widget_test.dart: Valida se snackBar está sendo exibida corretamente [E]
  Test failed. See exception logs above.
  The test description was: Valida se snackBar está sendo exibida corretamente

I need to be able to run all my tests together due to a customer requirement. I already have more than 23 tests ready and this is the first time this has happened.

Does anyone know why this is happening and a way to solve it ?

0 Answers
Related