0

I have a problem when running a whole widget test. I have 2 separated widget test and they are working fine when running individually but throwing error when being run simultaneously like this:

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following StateError was thrown running a test:
Bad state: No element

When the exception was thrown, this was the stack:
#0      Iterable.single (dart:core/iterable.dart:520:25)
#1      WidgetController.element (package:flutter_test/src/controller.dart:114:30)
#2      WidgetController.ensureVisible (package:flutter_test/src/controller.dart:1156:73)
#3      main.<anonymous closure>.<anonymous closure> 
<asynchronous suspension>
(elided one frame from package:stack_trace)
The test description was:
  Should show key of qty widget

Test failed. See exception logs above.
The test description was: Should show key of qty widget

the code:

import...

void main() {
  group(
    "Button Testing Widget",
    () {
      testWidgets(
          'Should return init of 1 and return 2 after incremented and return 1 after decremented',
          (WidgetTester qtyTester) async {
        await qtyTester.pumpWidget(
          MaterialApp(
              home: BlocProvider(
            create: (context) {
              return CartItemBloc([]);
            },
            child: OrderScreen(
              testing: true,
            ),
          )),
        );
        await qtyTester.pumpAndSettle();
        // print(find.byKey(Key('maincre')).evaluate());
        expect(find.text('1'), findsOneWidget);
        // print(find.byKey(ValueKey('mainQty')).evaluate());
        await qtyTester.ensureVisible(find.byKey(Key('mainIncrement')));
        await qtyTester.pumpAndSettle();
        await qtyTester.tap(find.byKey(Key('mainIncrement')));
        await qtyTester.pumpAndSettle();
        expect(find.text('2'), findsOneWidget);
        // print(find.byKey(ValueKey('mainQty')).evaluate());
        await qtyTester.tap(find.byKey(Key('mainDecrement')));
        await qtyTester.pumpAndSettle();
        expect(find.text('1'), findsOneWidget);
        // print(find.byKey(ValueKey('mainQty')).evaluate());
      });
      testWidgets('Should show key of qty widget',
          (WidgetTester cbTester) async {
        await cbTester.pumpWidget(MaterialApp(
          home: BlocProvider(
            create: (context) {
              return CartItemBloc([]);
            },
            child: OrderScreen(),
          ),
        ));
        await cbTester.pumpAndSettle();
        expect(find.byKey(Key('addQty-1')), findsNothing);
        await cbTester.ensureVisible(find.byKey(Key('chbox-1')));
        await cbTester.pumpAndSettle();
        await cbTester.tap(find.byKey(Key('chbox-1')));
        await cbTester.pumpAndSettle();
        expect(find.byKey(Key('addQty-1')), findsOneWidget);
      });
    },
  );
}

additional information when I move the first test to bottom the error now is on the qty button.

Raka Prasetyo
  • 57
  • 1
  • 6

0 Answers0