Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular test error: Cannot read property 'subscribe' of undefined at new RouterLinkWithHref

I don't get what I'm doing wrong, I didn't find any link of RouterLinkWithHref to any error in tests, furthermore I'm not using RouterLinkWithHref anywhere, as far as I know :/

Can anyone help me with this:

fdescribe('AddItemComponent: ', () => {
  let cmp: AddItemComponent;
  let fixture: ComponentFixture<AddItemComponent>;
  let de: DebugElement;
  let el: HTMLElement;

  const mockRouter = {
    navigate: jasmine.createSpy('navigate')
  };
  const mockCustomCardService = {
    getItemDeliveryDate: () => Observable.empty(),
    emitItemChanges: () => Observable.empty(),
  };
  const mockActivatedRoute = { snapshot: {} };
  const mockPageDataService = {};
  const mockUserService = {};

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [AddItemComponent],
      imports: [FormsModule, ReactiveFormsModule, SharedModule, TranslateModule.forRoot(), RouterTestingModule],
      providers: [
        SpinningService,
        FormBuilder,
        TranslateService,
        { provide: Router, useValue: mockRouter },
        { provide: CustomItemService, useValue: mockCustomItemService },
        { provide: PageDataService, useValue: mockPageDataService },
        { provide: UserService, useValue: mockUserService },
        { provide: ActivatedRoute, useValue: mockActivatedRoute }
      ],
      schemas: [NO_ERRORS_SCHEMA]
    });

    fixture = TestBed.createComponent(AddItemComponent);
    cmp = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('Simple test', () => {
    expect(true).toBeTruthy();
  });

});
like image 306
Donovant Avatar asked Aug 09 '17 11:08

Donovant


1 Answers

You should remove your routing mocks (mockRouter, mockActivatedRoute) since you already import RouterTestingModule, something like this:

describe('AddItemComponent: ', () => {
  let cmp: AddItemComponent;
  let fixture: ComponentFixture<AddItemComponent>;
  let de: DebugElement;
  let el: HTMLElement;

  const mockCustomCardService = {
    getItemDeliveryDate: () => Observable.empty(),
    emitItemChanges: () => Observable.empty(),
  };
  const mockPageDataService = {};
  const mockUserService = {};

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [AddItemComponent],
      imports: [FormsModule, ReactiveFormsModule, SharedModule, TranslateModule.forRoot(), RouterTestingModule],
      providers: [
        SpinningService,
        FormBuilder,
        TranslateService,
        { provide: CustomItemService, useValue: mockCustomItemService },
        { provide: PageDataService, useValue: mockPageDataService },
        { provide: UserService, useValue: mockUserService }
      ],
      schemas: [NO_ERRORS_SCHEMA]
    });

    fixture = TestBed.createComponent(AddItemComponent);
    cmp = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('Simple test', () => {
    expect(true).toBeTruthy();
  });

});
like image 188
sunsay Avatar answered Oct 16 '22 00:10

sunsay