Skip to content

Widget tests type is not registered inside GetIt for Lazy Singleton #252

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

Closed
martipello opened this issue Feb 3, 2022 · 1 comment
Closed

Comments

@martipello
Copy link

I'm having some issues with my widget tests but stumbled on to what i think is an issue with get_it

in my test i reset get_it and initialize dependencies, i then try to get an instance of a lazy singleton and get_it throws an error telling me it isn't registered but if i change it to a singleton it seems to work

Future<void> initTesting() async {
   //ERROR
    getIt.registerLazySingleton<ApiBaseClient>(() => ApiTestClient(getIt(), getIt(), getIt()), instanceName: 'ApiTestClient');
    //NO ERROR
    getIt.registerSingleton<ApiBaseClient>(ApiTestClient(getIt(), getIt(), getIt()), instanceName: 'ApiTestClient');
}

import 'package:wordskii2/dependency_injection_container.dart' as di;

testWidgets(
  'test',
      (tester) async {
          await tester.runAsync(di.getIt.reset);
          await tester.runAsync(di.initTesting);
          await tester.runAsync(di.getIt.allReady);
 
         final apiClient = di.getIt.get<ApiTestClient>();
});
@martipello
Copy link
Author

ok i think i got this to work using

    final apiClient = di.getIt.get<ApiBaseClient>(instanceName: 'ApiTestClient') as ApiTestClient;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants