Skip to content

[Usage question] Using GetIt.I.get() in integration test #265

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
CaptainDario opened this issue Mar 13, 2022 · 4 comments
Closed

[Usage question] Using GetIt.I.get() in integration test #265

CaptainDario opened this issue Mar 13, 2022 · 4 comments

Comments

@CaptainDario
Copy link

CaptainDario commented Mar 13, 2022

I am trying to use GetIt in my integration test but I am having some difficulties.
When trying to access registered singletons in GetIt, I am always facing this error:

_AssertionError ('package:get_it/get_it_impl.dart': Failed assertion: line 370 pos 7: 'instanceFactory != null': Object/factory with  type DrawScreenState is not registered inside GetIt. 
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
Did you forget to register it?))

I have a function to register my singletons:

// Initialize GetIt by initializing and registering all the instances for it
Future<void> initGetIt() async {
  
  ...

  // draw screen services 
  GetIt.I.registerSingleton<DrawScreenState>(DrawScreenState(
    Strokes(), KanjiBuffer(), DrawingLookup(), DrawScreenLayout.Portrait)
  );
  
  ...
}

This function is called when creating my app instance. However, when I call my function, the singletons are not registered in GetIt and the above Exception is thrown. My code looks like this:

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  testWidgets("DrawScreen test", (WidgetTester tester) async {

    IS_TESTING_DRAWSCREEN = true;

    // create app instance and wait until it finished initializing
    app.main();    // <-- this calls initGetIt()
    print(GetIt.I<DrawScreenState>());      // <-- this raises the exception
    await tester.pumpAndSettle(Duration(seconds: 2));

    // check that the app does not show any predictions on start up
    List<String> preds = (tester.widgetList(find.byType(PredictionButton))).map((e) => (e as PredictionButton).char).toList();

    ...
  }
}

In the README integration tests are mentioned, therefore I assume that it should be possible to access GetIt in them. This leads me to the question what am I doing wrong?

@CaptainDario
Copy link
Author

@escamoteur any ideas on this?

@escamoteur
Copy link
Collaborator

escamoteur commented Mar 24, 2022 via email

@CaptainDario
Copy link
Author

Your app init function is not awaited if I understand it correctly. Am 24. März 2022, 16:32 +0100 schrieb CaptainDario @.>:

@escamoteur any ideas on this? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.
>

Oh wow, Now I feel so stupid for asking this.
This literally kept me busy for days.

Thank you so much!

@escamoteur
Copy link
Collaborator

escamoteur commented Mar 24, 2022 via email

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

2 participants