injectable icon indicating copy to clipboard operation
injectable copied to clipboard

Mocking 1 chosen class

Open tremp-m opened this issue 2 years ago • 0 comments

Hi i am newbee in injectable, and i can't undestand how i can make one thing (and it's possible?) I want mock one chosen class (other class must be from prod). For example i have in lib directory:

@injectable
class ServiceB {
  final ServiceAInterface serviceAI;
  ServiceB(this.serviceAI);
  void about() {
    serviceAI.about();
  }
}

abstract class ServiceAInterface {
  void about();
}

@Singleton(as: ServiceAInterface)
class ServiceA implements ServiceAInterface {
  @override
  void about() {
    print('this is ServiceA');
  }
}

final getIt = GetIt.instance;

@InjectableInit(
  initializerName: r'$initGetIt', // default
  preferRelativeImports: true, // default
  asExtension: false, // default
)
void initDIMain() => $initGetIt(getIt);

in test directory:

@Singleton(as: ServiceAInterface, env: ['test'])
class ServiceATest implements ServiceAInterface {
  @override
  void about() {
    print('this is ServiceATest');
  }
}

@InjectableInit(
  generateForDir: ['lib', 'test'],
  initializerName: r'$initGetIt', // default
  preferRelativeImports: true, // default
  asExtension: false, // default
)
void initDITest() =>
    $initGetIt(testGetIt, environmentFilter: NoEnvOrContains(Environment.test));

it's not generate. And i think it'right. But is it possible replace only ServiceAInterface on ServiceATest in test? Without change prod enviroment?

tremp-m avatar Aug 09 '21 18:08 tremp-m