Easy_localization: Loading localization in unit tests

Created on 10 Jun 2020  路  5Comments  路  Source: aissat/easy_localization

What is the easy way of loading LocaleKeys in unit tests.

waiting for customer response

Most helpful comment

I've got a simplest solution

...

import 'package:easy_localization/src/translations.dart';

import 'package:easy_localization/src/localization.dart';

...

class TranslationsMock extends Mock implements Translations {}

...

setUpAll(() async {

    final translations = TranslationsMock();

    when(translations.get("my_awesome_key")).thenReturn("Test");

    Localization.load(Locale('en'), translations: translations);

  });

...

test(...)

...

@Overman775 it should be great to get this snippet in the plugin documentation in a test part ;-)

All 5 comments

@necipk you need LocaleKeys or localized keys?

In one of my unit test it throws exception with a text from LocaleKeys.mykey and as localization is not loaded in the unit test it gives an error.

I've got a simplest solution

...

import 'package:easy_localization/src/translations.dart';

import 'package:easy_localization/src/localization.dart';

...

class TranslationsMock extends Mock implements Translations {}

...

setUpAll(() async {

    final translations = TranslationsMock();

    when(translations.get("my_awesome_key")).thenReturn("Test");

    Localization.load(Locale('en'), translations: translations);

  });

...

test(...)

...

@Overman775 it should be great to get this snippet in the plugin documentation in a test part ;-)

I've got a simplest solution

...

import 'package:easy_localization/src/translations.dart';

import 'package:easy_localization/src/localization.dart';

...

class TranslationsMock extends Mock implements Translations {}

...

setUpAll(() async {

    final translations = TranslationsMock();

    when(translations.get("my_awesome_key")).thenReturn("Test");

    Localization.load(Locale('en'), translations: translations);

  });

...

test(...)

...

@Overman775 it should be great to get this snippet in the plugin documentation in a test part ;-)

You should really think about adding this to the README.md since I almost dropped your lib because I could not find a solution at first!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jerrywyx picture Jerrywyx  路  4Comments

EmmanuelMess picture EmmanuelMess  路  7Comments

Xgamefactory picture Xgamefactory  路  4Comments

enricoquarantini picture enricoquarantini  路  4Comments

Overman775 picture Overman775  路  6Comments