我没有弄错我在做什么,我没有找到 RouterLinkWithHref 到测试中任何错误的任何链接,而且据我所知,我不在任何地方使用RouterLinkWithHref:/

谁能帮我这个:

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();
  });

});

最佳答案

由于您已经导入了 RouterTestingModule ,因此应删除路由模拟( mockRouter mockActivatedRoute ),如下所示:

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();
  });

});

关于 Angular Testing 错误: Cannot read property 'subscribe' of undefined at new RouterLinkWithHref,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45589962/

10-09 17:54