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

Integration tests - get_it factories map is lost #76

Closed
yanivshaked opened this issue May 7, 2020 · 6 comments
Closed

Integration tests - get_it factories map is lost #76

yanivshaked opened this issue May 7, 2020 · 6 comments

Comments

@yanivshaked
Copy link

yanivshaked commented May 7, 2020

Hi,
In short: Problem is that registering several factories and singletones on get_it from the integration tests environment (e.g. app.dart file) has no effect and when the application starts running, the internal factories are lost.

In details: Using initServices() on the operational program, I am binding several abstract classes as follows:

GetIt.I.registerLazySingleton<INetworkService>(() => NetworkService()); GetIt.I.registerLazySingleton<ILocalization>(() => LocalizationService());

however, when using the integration tests environment, I would like to bind other implementations, so I'm calling initiServicesTestEnv() before running the app:

GetIt.I.registerLazySingleton<INetworkService>(() => NetworkServiceSimulator()); GetIt.I.registerLazySingleton<ILocalization>(() => LocalizationMock());

but when the test starts running, the following statement fails:

GetIt.I<INetworkService>();

due to the following:

No type INetworkService 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?)
'package:tw_app/get_it/get_it_impl.dart':
Failed assertion: line 251 pos 14: 'instanceFactory != null'

@escamoteur
Copy link
Collaborator

Can you show me the test file where you call GetIt.I<INetworkService>();

@escamoteur
Copy link
Collaborator

Hi,
could you check that the import statements for INetworkService is not relative but a package reference at all places you use it?

@yanivshaked
Copy link
Author

yanivshaked commented May 11, 2020

I'm not calling the GetIt.I<INetworkService>(); from the test file. Only from the application itself.
The test consist of two files, 1st one is responsible for starting the application under test and the 2nd contains the test itself. The 1st file is:


import 'package:get_it/get_it.dart';
import 'package:flutter/material.dart';
import 'package:flutter_driver/driver_extension.dart';
import 'package:tw_app/main.dart' as app;

import '../lib/Services/Network/NetworkServiceSimulator.dart';
import '../lib/Services/Logger/ConsoleLoggerService.dart';
import '../lib/Services/Localization/LocalizationService.dart';

void main() {
  print('Test environment wakeup');
  // This line enables the extension.
  enableFlutterDriverExtension();

  // TODO: Pending answer on https://github.com/fluttercommunity/get_it/issues/76
  initServicesTestEnv();
  runApp(app.MyApp());
}

void initServicesTestEnv() {
  GetIt.I.registerLazySingleton<INetworkService>(() => NetworkServiceSimulator());
  GetIt.I.registerLazySingleton<ILogger>(() => ConsoleLoggerService());
  GetIt.I.registerLazySingleton<ILocalization>(() => LocalizationService());
}

and as soon as the application starts running (from within runApp), the first call to GetIt.I<INetworkService>(); fails.

@yanivshaked
Copy link
Author

Hi,
could you check that the import statements for INetworkService is not relative but a package reference at all places you use it?

After converting all import statements for all abstract classes to package references, the problem is solved.
Can you please elaborate on the reason for this? Does it mean I have to use package reference from now on, or will there be a fix for this?

@escamoteur
Copy link
Collaborator

Please have a look at the explanation here dart-lang/sdk#41868

@yanivshaked
Copy link
Author

Thank you!

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