Closed muzzah closed 1 month ago
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.
im pretty sure I tried that as well and got a siimilar error message
Nope, that's just not possible because the error you got is that the registration could not be found. You have to use the same type you used when registering. You don't need an instanceName if you don't register more than one factory with that type. Am 27. Aug. 2024, 08:49 +0100 schrieb Mustafa @.***>:
im pretty sure I tried that as well and got a siimilar error message — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>
did you solve this?
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 testGetIt.instance.registerFactoryParam<AuthClient, GoogleSignIn, Null>((param1, param2) => authClient, instanceName: "authClient" );
and
authClient
here is a MockAuthClient object. The code being tested does the followingAuthClient 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
Though I am getting the following error
If I change
await GetIt.instance.unregister(instanceName: "authClient");
toawait GetIt.instance.unregister(instance: authClient);
in my unit test then the error changes slightly.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.