angularkarma-jasmineangularjs-injector

Can we configure Injector in TestBed


I am writing test cases for custom ErrorHandler in Angular. In the constructor I added the dependency of Injector as I learnt that the providers get initialized after ErrorHandler. This is indicated below:

export class ApplicationErrorHandler implements ErrorHandler {
    constructor(injector: Injector) {
       this.injector = injector;
    }
}

Now while writing test cases, how should I create my error Handler. Can I add Injector as a provider or can I do it as below:

errorHandler = injector.get(ApplicationErrorHandler);

In any case, I get errorHandler as undefined and the below error:

Error: Unexpected value '[object Object]' imported by the module 'DynamicTestModule'

I appreciate any help on how to write it in a way that I can test it. Thank you!


Solution

  • The above issue usually occurs, when you wrongly provide Providers inside Module and vice versa. Just double check if you have imported Modules Correctly under Modules and services under providers.

    Related reference.