Skip to content

reset changed to async? #159

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
sowens-csd opened this issue Feb 12, 2021 · 5 comments
Closed

reset changed to async? #159

sowens-csd opened this issue Feb 12, 2021 · 5 comments

Comments

@sowens-csd
Copy link

Thanks for the package, it's been a pleasure to work with. This is just a minor documentation issue for users upgrading from previous versions. The change to make reset async (I think that's a change?) broke a number of unit tests because instances were already registered since I wasn't doing an await for the method to complete before trying to register again. A note in the README could perhaps help reduce the confusion.

Another small point that I ran across is that I was setting up test dependencies in the setupAll method of my unit test. The dependencies were not then found by get_it in the setup method. This used to work and does not work anymore, I'm not sure why.

@escamoteur
Copy link
Collaborator

Hi, yes that has, changes, sorry if this caused trouble for you.

For the second part I would need to see your test file.

@sowens-csd
Copy link
Author

Thanks for the quick response. I see what happened for the setupAll issue now. I tried to recreate it and the test passed, then I realized it's the same issue with not waiting for the reset. Here's a reproduce that shows the failure and is doing what my code used to do:

import 'package:get_it/get_it.dart';
import 'package:test/test.dart';

class ToRegister {}

void main() {
  final sl = GetIt.instance;
  ToRegister registered;

  setUpAll(() {
    sl.reset();
    sl.registerSingleton<ToRegister>(ToRegister());
  });

  setUp(() {
    registered = sl<ToRegister>();
  });

  test('Registered is found', () {
    expect(registered, isNotNull);
  });
}

And here's the working version:

import 'package:get_it/get_it.dart';
import 'package:test/test.dart';

class ToRegister {}

void main() {
  final sl = GetIt.instance;
  ToRegister registered;

  setUpAll(() async {
    await sl.reset();
    sl.registerSingleton<ToRegister>(ToRegister());
  });

  setUp(() {
    registered = sl<ToRegister>();
  });

  test('Registered is found', () {
    expect(registered, isNotNull);
  });
}

@escamoteur
Copy link
Collaborator

Ah, good to hear. then I can close this issue.
Btw. do you know that there is a compagnion package to get_it. the get_it_mixin?

@sowens-csd
Copy link
Author

I did not, I just had a look at get_it_mixin, it look's interesting, thanks for the pointer, and for your work.

@escamoteur
Copy link
Collaborator

If you really want to go a bit further also check out flutter_command and functional_listener:-)

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