2

I'm trying to create a unit test and test the router of a component. I think I'm nearly there but I run into the error

'TypeError: Cannot read property 'subscribe' of undefined at new RouterLinkWithHref'

and I did not find the solution on the web...

I have the following setup in my spec file:

describe('TestOverviewComponent', () => {
  let comp:    TestOverviewComponent;
  let fixture: ComponentFixture<TestOverviewComponent>;
  let createEl:      DebugElement;
  let routerStub;

  beforeEach(async(() => {
    routerStub = {
      navigate: jasmine.createSpy('navigate'),
      routerState: {}
    };

    TestBed.configureTestingModule({
      declarations: [], // declare the test component
      imports: [
        NgReduxTestingModule,
        HttpClientTestingModule,
        TranslateModule.forRoot(),
        NgbModule.forRoot(),
        DialogSharedModule,
        RouterTestingModule,
        TestModule
      ],
      providers: [
        { provide: Router, useValue: routerStub },
        SessionService,
        Logger,
        StorageService,
      ]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(TestOverviewComponent);
    comp = fixture.componentInstance;
    createEl = fixture.debugElement.query(By.css('.btn-primary'));

    comp.paginationFilter.page = 1;
    comp.paginationFilter.pageSize = 10;

    fixture.detectChanges();
  });

  it('should be created', () => {
    expect(comp).toBeTruthy();
  })
Nicholas
  • 1,189
  • 4
  • 20
  • 40
  • Remove router from providers because you are using RouterTestingModule. Please see https://stackoverflow.com/questions/45589962/angular-test-error-cannot-read-property-subscribe-of-undefined-at-new-routerl and https://stackoverflow.com/questions/39791773/angular-2-unit-testing-with-router/40187305#40187305 – Shailesh Vikram Singh Feb 26 '18 at 06:11

0 Answers0