Skip to content

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

Closed
@muzzah

Description

@muzzah

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions