1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter Unit Test With Localization

Discussão em 'Mobile' iniciado por Stack, Novembro 7, 2024 às 16:12.

  1. Stack

    Stack Membro Participativo

    I have a utility class that relies on the context in order to translate the output. Something along the lines of :

    static String doSomeLogicAndTranslate(String firstArg, BuildContext context) {
    //...
    return AppLocalizations.of(context)!.someKey;
    }


    Now, I want to be able to write some unit tests for this class, but since the second argument is the context, I'm having a hard time doing so.

    What I have tried so far:


    • Mocking a BuildContext with Mockito (as described here) and passing that as the second argument --> Doesn't work as inside the localizations.dart file it returns null here

      static T? of<T>(BuildContext context, Type type) {
      final _LocalizationsScope? scope = context.dependOnInheritedWidgetOfExactType<_LocalizationsScope>(); /// Scope is null here
      return scope?.localizationsState.resourcesFor<T?>(type);
      }

    I have tried looking around for any other solutions for this, but have not found anything substantial. This seems like it should be pretty straight forward, but it isn't.

    Continue reading...

Compartilhe esta Página