2

I am new to karma and jasmine, so please forgive me if this sounds silly. I have these following code and what I want to do with it, is finding out what I need to do next. I have CUSTOM_HTTP_CONFIG injected in the constructor in hello-http.service.ts, which I cannot find tutorial how I can achieve this, manual inject the dependency and that is what I think the error message is complaining about.

test.spec.ts

beforeEach(async(() => {
 TestBed.configureTestingModule({
  declaration: [...],
  imports[RouterTestingModule, ...],
  providers: [HelloHttpService, ...],
 });
});

hello-http.service.ts

constructor(
 @Inject(CUSTOM_HTTP_CONFIG) protect config: CustomHttpParams,
 ...
) {
 super(config, http);
}

Karma: error

Failed: Uncaught (in promise): Error: StaticInjectorError(DynamicTestModule)[HelloHttpService -> InjectionToken Custom HTTP Config]: 
  StaticInjectorError(Platform: core)[HelloHttpService -> InjectionToken Custom HTTP Config]: 
    NullInjectorError: No provider for InjectionToken Custom HTTP Config!
Error: StaticInjectorError(DynamicTestModule)[HelloHttpService -> InjectionToken Custom HTTP Config]: 
  StaticInjectorError(Platform: core)[HelloHttpService -> InjectionToken Custom HTTP Config]: 
    NullInjectorError: No provider for InjectionToken Custom HTTP Config!
    at _NullInjector.webpackJsonp../node_modules/@angular/core/esm5/core.js._NullInjector.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/esm5/core.js:1003:1)
    at resolveToken (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/esm5/core.js:1301:1)
roger
  • 1,225
  • 2
  • 17
  • 33

2 Answers2

3

Try something like this:

import { TestBed, inject } from '@angular/core/testing';

import { HelloHttpService } from './http.service';

describe('HttpService', () => {
    let service : HelloHttpService;

    // mock the service, there we no need to pull in any dependency or need to declare constant for the injector aka CUSTOM_HTTP_CONFIG
    const mockHelloHttpService() = { } as HelloHttpService;

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [{
             provide: HelloHttpService,
             useValue: mockHelloHttpService, // must do this, or it will try to find the injector token in the custom http service
             // which the reason why this error occurred.
         }],
        });
    });
it('should be created', inject([HelloHttpService], (service: HelloHttpService) => {
    expect(service).toBeTruthy();
}));
});
roger
  • 1,225
  • 2
  • 17
  • 33
Akj
  • 7,038
  • 3
  • 28
  • 40
  • That was the one of the methods i tried before, it complained on this line `fixture = TestBed.createComponent(LogoutComponent);` and I assumed it needs all of dependencies. From your method I should only inject the service when I need it, but I run it before each test? – roger Aug 23 '18 at 11:02
0

You can try the following :-

let svc: HelloHttpService;    

beforeEach(inject([HelloHttpService], (serviceArg: HelloHttpService) => {
    svc = serviceArg;
}));