easy_localization icon indicating copy to clipboard operation
easy_localization copied to clipboard

Bad state: No element - Widget tests fails

Open EliasDolinsek opened this issue 3 years ago • 3 comments

I've written a widget test for my welcome page, four in total. The page basically only contains buttons for sign in, register and skip.

  testWidgets(
      "Show error when legal notice is not checked but sign up is clicked",
      (WidgetTester tester) async {
    await tester.runAsync(() async {
      await tester.pumpWidget(
        const BaseTestWidget(
          child: WelcomePage(),
        ),
      );

      await tester.idle();
      await tester.pumpAndSettle();
    });

    await tester.tap(find.widgetWithText(RaisedButton, "SIGN UP"));
    await tester.pumpAndSettle();

    expect(
      find.byType(SnackBar),
      findsOneWidget,
    );
  });

BaseTestWidget is a widget purely for testing and looks like the following:

class BaseTestWidget extends StatelessWidget {
  final Widget child;

  const BaseTestWidget({Key key, this.child}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return EasyLocalization(
      supportedLocales: [const Locale("en")],
      path: "assets/lang/",
      fallbackLocale: const Locale("en"),
      saveLocale: false,
      startLocale: const Locale("en"),
      child: MaterialApp(
        home: child,
      ),
    );
  }
}

As soon as running the test, the following error gets shown. It seems like it fails to find the localization keys.

[🌎 Easy Localization] [DEBUG] Start
[🌎 Easy Localization] [DEBUG] Init state
[🌎 Easy Localization] [INFO] Start locale loaded en
[🌎 Easy Localization] [DEBUG] Build
[🌎 Easy Localization] [DEBUG] Init Localization Delegate
[🌎 Easy Localization] [DEBUG] Init provider
[🌎 Easy Localization] [WARNING] Localization key [app_name] not found
[🌎 Easy Localization] [WARNING] Localization key [cb_auth_legal_notice] not found
[🌎 Easy Localization] [WARNING] Localization key [btn_auth_sign_up] not found
[🌎 Easy Localization] [WARNING] Localization key [btn_auth_use_anonymously] not found
[🌎 Easy Localization] [WARNING] Localization key [btn_auth_sign_in] not found
══╡ 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:498:25)
#1      WidgetController._getElementPoint (package:flutter_test/src/controller.dart:814:47)
#2      WidgetController.getCenter (package:flutter_test/src/controller.dart:786:12)
#3      WidgetController.tap (package:flutter_test/src/controller.dart:257:18)
#4      main.<anonymous closure> (file:///Users/eliasdolinsek/Development/AcGoals/test/presentation/auth/welcome_page_test.dart:22:18)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)

The test description was:
  Show error when legal notice is not checked but sign up is clicked
════════════════════════════════════════════════════════════════════════════════════════════════════

EliasDolinsek avatar Mar 18 '21 10:03 EliasDolinsek

@EliasDolinsek do you found any solution?

kjawadDeveloper1 avatar May 10 '21 11:05 kjawadDeveloper1

@kjawadDeveloper1 Unfortunately not

EliasDolinsek avatar May 11 '21 08:05 EliasDolinsek

Follow the examples here https://github.com/aissat/easy_localization/blob/develop/test/easy_localization_widget_test.dart to get widget tests to work.

There are some things I dislike, you have to run your tests in runAsync, and you have to call tester.pump() once after tester.pumpWidget() as it is required by runAsync tests.

biscottis avatar Oct 08 '21 15:10 biscottis