mardi 30 juillet 2019

Widget tests in Flutter work individually but not all together

Hello everyone and thank you already for your time.

I'm trying to run tests on my widgets. Most of my tests work but the first widget test I wrote that need to get a context with mocked providers fails runned together. But if I run them individually, it works well.

I suppose some things in my context don't reset well or it feels like it doesn't have time to rebuild correctly my widget to test it. I tried to make a variable used in each test containing my context. I tried then to tear it down but nothing solved my problem.

utils.dart

class MockThemeProvider extends Mock implements ThemeProvider {}

Widget getMainContext({Widget child, AuthService authService}) {
  AppTranslationsDelegate _newLocaleDelegate =
      AppTranslationsDelegate(newLocale: Locale('fr', ''));

  var _env = Env(
    apiUrl: 'http://url_lynou_test.com',
    deviceId: 1,
    accessTokenKey: 'accessTokenKey',
    refreshTokenKey: 'refreshTokenKey',
    expiresInKey: 'expiresInKey',
  );

  // add Services or Mocked services
  var _authService = authService == null ? AuthService(env: _env) : authService;

  return MultiProvider(
    providers: [
      Provider<AuthService>.value(
        value: _authService,
      ),
      ChangeNotifierProvider<MockThemeProvider>.value(
        value: MockThemeProvider(),
      ),
    ],
    child: MaterialApp(
      theme: ThemeData(
        primaryColor: Colors.red,
      ),
      localizationsDelegates: [
        _newLocaleDelegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale('en', ''), // English
        const Locale('fr', ''), // French
      ],
      home: child,
    ),
  );
}

login_screen_test.dart

class MockAuthService extends Mock implements AuthService {
  var env = Env(
    apiUrl: 'http://url_lynou_test.com',
    deviceId: 1,
    accessTokenKey: 'accessTokenKey',
    refreshTokenKey: 'refreshTokenKey',
    expiresInKey: 'expiresInKey',
  );

  // Mock up login
  Future<void> login(String email, String password) async {}
}

void main() {

  testWidgets('LoginScreen - Displaying', (WidgetTester tester) async {
    await tester.pumpWidget(getMainContext(
      child: LoginScreen(),
      authService: MockAuthService(),
    ));
    await tester.pumpAndSettle();

    var findByLoginScreen = find.byType(LoginScreen);
    expect(findByLoginScreen.evaluate().isEmpty, false);
  });

  testWidgets('LoginScreen - Click on login with empty inputs',
      (WidgetTester tester) async {
    await tester.pumpWidget(
      getMainContext(
        child: LoginScreen(),
        authService: MockAuthService(),
      ),
    );
    await tester.pumpAndSettle();

    var findByLoginScreen = find.byType(LoginScreen);
    expect(findByLoginScreen.evaluate().isEmpty, false); // Can't find the Login Screen

    var findByLoginButton = find.byType(RoundedButton);
    expect(findByLoginButton.evaluate().isEmpty, false);
     await tester.tap(findByLoginButton.first);
  });
}

On the second test, it can't even find again my LoginScreen widget when it succeed on the first test with the same code. I don't understand what I'm doing wrong.

For more context, here is my code: https://github.com/Appli-chic/lynou/tree/dev

Aucun commentaire:

Enregistrer un commentaire