Skip to content

No type X is registered inside GetIt #77

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
bvdwalt opened this issue May 9, 2020 · 5 comments
Closed

No type X is registered inside GetIt #77

bvdwalt opened this issue May 9, 2020 · 5 comments

Comments

@bvdwalt
Copy link

bvdwalt commented May 9, 2020

Hi guys,
I'm only having issues using get_it when running tests in my Flutter app, otherwise it works perfectly. When registering the services I use a mock client, but that's not even the part giving a problem. The test which essentially runs the whole app, 'App loads flight correctly', succeeds, the only difference I can tell is that the one is a testWidget test (works), and the failing ones are normal tests.

Kindly see below errors I am getting, first it says that there is no type registered inside Getit and then when the next test runs and I try to register all the services again it suddenly says that the same type is already registered. I only did the second registering to try and see if it would make any difference.

My GitHub repo containing the below code as well as the full app can be found here.

Thanks in advance for anyone able to assist.

Registering code

final getIt = GetIt.instance;

void registerServices({bool testing = false}) {
  //getIt.reset();
  getIt.registerLazySingleton(() => FlightProvider());

  if (!testing) {
    getIt.registerLazySingleton<http.Client>(() => Client());
  } else {
    getIt.registerLazySingleton<http.Client>(() => MockClient());
  }
}

class MockClient extends Mock implements http.Client {}

Errors on flutter test

PS C:\Users\BennieVanDerWalt\source\spacex_flights> flutter test
00:08 +0 -1: C:\Users\BennieVanDerWalt\source\spacex_flights\test\FlightDataService_test.dart: fetchFlightData returns a flight if the http call completes successfully [E]                                                                  
  'package:get_it/get_it_impl.dart': Failed assertion: line 251 pos 14: 'instanceFactory != null': No type FlightProvider is registered inside GetIt.
   Did you forget to pass an instance name?
  (Did you accidentally do  GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;did you forget to register it?)
  dart:core                                  _AssertionError._throwNew
  package:get_it/get_it_impl.dart 251:14     _GetItImplementation._findFactoryByNameOrType
  package:get_it/get_it_impl.dart 263:27     _GetItImplementation.get
  ..\lib\src\resources\repository.dart 8:32  new Repository
  FlightDataService_test.dart 15:27          main.<fn>.<fn>

00:08 +0 -2: C:\Users\BennieVanDerWalt\source\spacex_flights\test\FlightDataService_test.dart: fetchFlightData throws an exception if the http call completes with an error [E]
  Invalid argument(s): Type FlightProvider is already registered
  package:get_it/get_it_impl.dart 6:18   throwIf
  package:get_it/get_it_impl.dart 547:5  _GetItImplementation._register
  package:get_it/get_it_impl.dart 403:5  _GetItImplementation.registerLazySingleton
  ..\lib\service_locator.dart 11:9       registerServices
  FlightDataService_test.dart 25:7       main.<fn>.<fn>

00:11 +1 -2: Some tests failed.
@yanivshaked
Copy link

I'm having the same issue - for now, the workaround is to transfer the boolean parameter (test) to the application running inside runApp() and use it there to initialize your services.
(Obviously this is a temporary workaround, waiting to the developer to analyze this)

@escamoteur
Copy link
Collaborator

I just replaced the import statements from relative to import 'package:spacex_flights/src/resources/flight_provider.dart';
Then GetIt works fine but I get another Exception that comes probably from Mocito.

@bvdwalt
Copy link
Author

bvdwalt commented May 12, 2020

Thanks for you input @yanivshaked, but I went for the suggestion by @escamoteur.

I changed the following import and some others to the package import style:
from:
import '../models/flights.dart';
to:
import 'package:spacex_flights/src/models/flights.dart';

@escamoteur, I'm not that clued up on Flutter just yet, so is the 'package:' import style the best way to do imports or is this because of some get_it internal workings that it works better that way?

Thanks for your time and looking at my code, my latest commit on my repo has fixed all the tests.

@bvdwalt bvdwalt closed this as completed May 12, 2020
@escamoteur
Copy link
Collaborator

Please read this explanation here dart-lang/sdk#41868

I always recommend using package imports

@bvdwalt
Copy link
Author

bvdwalt commented May 14, 2020

Thanks @escamoteur, I have read the other issue and will look at implementing that. I really appreciate the effort.

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

3 participants