Skip to content
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

Problem when unregistering factory which has a subclass return type #371

Open
muzzah opened this issue Aug 25, 2024 · 4 comments
Open

Problem when unregistering factory which has a subclass return type #371

muzzah opened this issue Aug 25, 2024 · 4 comments

Comments

@muzzah
Copy link

muzzah commented Aug 25, 2024

So Im trying to unit test some code. The code uses an extension method which mockito cannot mock. So what Ive tried to do is the following with the help of GetIt.

The method in question is googleSignIn.authenticatedClient which returns an AuthClient. So I have created a factory registration in my unit test

GetIt.instance.registerFactoryParam<AuthClient, GoogleSignIn, Null>((param1, param2) => authClient, instanceName: "authClient" );

and authClient here is a MockAuthClient object. The code being tested does the following

AuthClient client = DIService.get<AuthClient>(param1: _googleSignIn);

Now this all works well on the happy path. Thing is googleSign.authenticatedClient can return null but GetIt doesnt support null returns. I need to handle this situation in my code path so what I thought Id do is wrap this code in a try/catch and then try to have GetIt throw an exception (simulating a null return type problem that would occur at runtime if null was returned). To do this I do the following in my test case

await GetIt.instance.unregister(instanceName: "authClient");
GetIt.instance.registerFactoryParam<AuthClient, GoogleSignIn, Null>((param1, param2) => throw Error() );

Though I am getting the following error

dart:core                                               _AssertionError._throwNew
package:get_it/get_it_impl.dart 404:7                   _GetItImplementation._findFirstFactoryByNameAndTypeOrNull
package:get_it/get_it_impl.dart 433:9                   _GetItImplementation._findFactoryByNameAndType
package:get_it/get_it_impl.dart 1323:11                 _GetItImplementation.unregister
test/operations/user/account_provider_test.dart 254:30  main.<fn>.<fn>.<fn>

'package:get_it/get_it_impl.dart': Failed assertion: line 404 pos 7: 'type != null || const Object() is! T': GetIt: The compiler could not infer the type. You have to provide a type and optionally a name. Did you accidentally do `var sl=GetIt.instance();` instead of var sl=GetIt.instance;

If I change await GetIt.instance.unregister(instanceName: "authClient"); to await GetIt.instance.unregister(instance: authClient); in my unit test then the error changes slightly.

package:get_it/get_it_impl.dart 9:18                    throwIf
package:get_it/get_it_impl.dart 1417:5                  _GetItImplementation._findFactoryByInstance
package:get_it/get_it_impl.dart 1322:11                 _GetItImplementation.unregister
test/operations/user/account_provider_test.dart 254:30  main.<fn>.<fn>.<fn>

Bad state: This instance of the type MockAuthClient is not available in GetIt If you have registered it as LazySingleton, are you sure you have used it at least once?

If I set GetIt.instance.allowReassignment = true; then I dont need to worry about unregistering and all seems to work ok.

Not sure if this is a bug or just a constraint of the getIt library but thought Id check to see if I was doing something wrong.

@escamoteur
Copy link
Collaborator

you can't unregister just by using an instanceName, you have to provide the type that was used to register it.
throwing an exception inside the factory function should be fine.

@muzzah
Copy link
Author

muzzah commented Aug 27, 2024

im pretty sure I tried that as well and got a siimilar error message

@escamoteur
Copy link
Collaborator

escamoteur commented Aug 27, 2024 via email

@escamoteur
Copy link
Collaborator

did you solve this?

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