Milad-Akarie / injectable

Code Generator for get_it
MIT License
528 stars 140 forks source link

How to use Mock classes with injectable (Documentation is unclear)? #391

Open Vildnex opened 10 months ago

Vildnex commented 10 months ago

I've read the documentation few times already and so far I understand is that I can use injectable for anything which is is "lib" folder, or any other folder if is specify by generateForDir on @InjectableInit but here is the problem... I can not access anything which is into the test folder, in my case this been all the mocks for all the classes.

Example (If I already have the 2 classes inside my lib folder):

abstract class Service {
  void doSomething();
}

@LazySingleton(as: Service, env: [Environment.dev])
class ServiceA implements Service {
  @override
  void doSomething() {
    print("ServiceA");
  }
}

@LazySingleton(as: Service, env: [Environment.test])
class ServiceB implements Service {
  @override
  void doSomething() {
    print("ServiceA");
  }
}

# and the call it like this:
getIt<Service>().doSomething()

But in my case I am using a 3d party library for my backend service called [Parse Server](https://pub.dev/packages/parse_server_sdk). I am not sure how exactly to do:

  1. I have a class ParseObject from this library and also a Mock class for this (which is inside of test folder. How can I create a injection, such that I can call getIt<ParseObject>().doSomething() and return a MockParseObject if it is test enviroment, or a ParseObject if is a dev enviroment.

  2. Because when I initialize the injectable, I have to tell him this part "@LazySingleton(as: Service, env: [Environment.test])" or any other in this regard, how can I keep my Mock inside in my test folder without interfiring with the lib folder?

  3. Is there any alternative of doing it into a much nice form then this (which also dose not work because I cant import the Mock class from test folder into the lib folder)

@LazySingleton()
class ParseObjectFactory {
  ParseObject create(String className) {
    if (currentEnvironment == 'test') {
      return MockParseObject();
    } else {
      return ParseObject(className);
    }
  }
}

getIt<ParseObjectFactory>().create(_className)

I've try to find any example for this situations but I think either the documentation is not clear enought, or I think I am missing something. Could any of you guys help me whit this?

Milad-Akarie commented 10 months ago

Hey @Vildnex have you considered using a register module to register your third-party dependencies?

Vildnex commented 10 months ago

@Milad-Akarie sorry for the delay. I'm not sure how to do this or what exactly do you mean, could you give me an example?

github-actions[bot] commented 9 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions

Vildnex commented 8 months ago

@Milad-Akarie so could you provide me an example of any sort? I still have this issue and I could not find anything on how to fix it.

Milad-Akarie commented 8 months ago

Hey @Vildnex sorry for the delay I've been too busy. Why not do it the other way around? keep your lib folder clean of mocks and inside the test folder generate your mocks with everything else from the lib folder

inside of your test folder:

final getIt = GetIt.instance;

@InjectableInit(
  generateForDir: ['lib', 'test'], // process files inside both folders
  preferRelativeImports: true, // because classes inside of the test folder can not be package-imports
  initializerName: 'initTest' // optional name to avoid confusion with the main init method
)
void main() async {
  await getIt.initTest(environment: Environment.test);
  print(getIt<MockableService>());
}

@test
@Injectable(as: MockableService)
class ServiceMock extends MockableService{
  void doSomething() {
    print('mocked');
  }
}
github-actions[bot] commented 7 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions

Vildnex commented 7 months ago

@Milad-Akarie sorry also for this delay this the right thing that I needed. Thank you so much for help 😁🎉

github-actions[bot] commented 6 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions