mardi 2 juin 2020

Angular testing : RouterTestingModule

I'm new in Angular tests and I have an error on every component that is using a navigation Service :

Failed: StaticInjectorError(DynamicTestModule)[NavigationService -> Router]: 
  StaticInjectorError(Platform: core)[NavigationService -> Router]: 
    NullInjectorError: No provider for Router!

The navigation Service uses the angular router. When I add the RoutertestingModule on the imports of a component's spec file, I don't have the error anymore. I was wondering if the solution was to add the RouterTestingModule on each component's spec file or if there was something I missed ?

navigation.service.spec.ts

import { TestBed, inject } from '@angular/core/testing';
import { NavigationService } from './navigation.service';
import { RouterTestingModule } from '@angular/router/testing';
import { HubConnectionService } from '../hub-connection/hub-connection.service';
import { LoaderService } from '../loader.service';

describe('NavigationService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports:[RouterTestingModule],
      providers: [NavigationService,HubConnectionService,LoaderService],
    });
  });

  it('should be created', inject([NavigationService], (service: NavigationService) => {
    expect(service).toBeTruthy();
  }));
});

Aucun commentaire:

Enregistrer un commentaire