I have quite a big class hierarchy in my project and I had to extract much of the duplicated code to parent class that would be extended, having children and even grandchildren. Parent class -> child class -> child of child class. Something like this. Trying to avoid duplicating constructors and super() call I used Injector.create({options}) in the parent class to inject some of the required services. The problem is now that all my tests are failing with TypeError: Cannot read property 'get' of undefined. Here is what I have:
export abstract class ParentClass implements OnInit {
protected router: Router;
protected constructor() {
this.router = AppModuleServices.injector.get(Router);
}
}
Now the AppModuleServices is just a wrapping class around the injector in order to have it used in my whole project.
export class AppModuleServices {
static injector: Injector;
}
I'm setting the static injector in the app's module like this:
export class SomeModule {
constructor(private injector: Injector) {
AppModuleServices.injector = this.injector;
}
}
Everything works just fine, it's correctly doing it's job but the UT are all failing.
I've tried Angular 2 instantiate global injector in parent in Karma Jasmine test but with no success Does anyone know what is to be done on the spec side to have this work?
Thank you
UPDATE: After debugging the tests I found that actually the error is:
ReferenceError: AppModuleServices is not defined
Info: AppModuleServices is not actually a module. It's just an exported class, if it matters. I've tried to put the injector into the actual app module, but trying to use it lower in the hierarchy creates a circular dependency. But still, as it works while running the application, I don't think that is the problem, something about mocking the injector should be my solution.