Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Widget using Plural unable to be tested #745

Open
lockieRichter opened this issue Jan 31, 2025 · 0 comments
Open

Widget using Plural unable to be tested #745

lockieRichter opened this issue Jan 31, 2025 · 0 comments

Comments

@lockieRichter
Copy link

I am unable to run widget tests for a widget that uses the plural extension method.

The tests are failing with the following error

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following LateError was thrown building _OpenWorkCount(dirty):
LateInitializationError: Field '_locale@2908425997' has not been initialized.
...
When the exception was thrown, this was the stack:
#0      Localization._locale (package:easy_localization/src/localization.dart)
#1      Localization.plural (package:easy_localization/src/localization.dart:152:36)
#2      plural (package:easy_localization/src/public.dart:114:31)
#3      StringTranslateExtension.plural (package:easy_localization/src/public_ext.dart:104:10)
...

When I change the widget to use just a standard translation with the .tr extension method the tests pass.

Any help on how to set up my tests so that the locale gets initialised would be great.

Right now I am doing the following in my tests.

void main() async {
  testWidgets('Test', (tester) async {
    TestWidgetsFlutterBinding.ensureInitialized();
    SharedPreferences.setMockInitialValues({});
    EasyLocalization.logger.enableBuildModes = [];
    await EasyLocalization.ensureInitialized();
    await tester.pumpWidget(
      EasyLocalization(
        path: 'assets/translations',
        supportedLocales: const [
          Locale('en', 'US'),
        ],
        child: MaterialApp(
          home: WigetUnderTest(),
        ),
      ),
    );
  });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant