подигравателна "тръба" в тестове за жасмин и TypeError: Не може да се прочете свойството "тръба" на undefined

Имам проблем с тестовете. Получавам грешка: TypeError: Cannot read property 'pipe' of undefined

метод в директивата:

@HostListener('mouseenter')
  onMouseenter(): void {
    forkJoin([
      this.dateService.formatDate(dateStart).pipe(take(1)),
      this.dateService.formatDate(dateEnd).pipe(take(1))
    ]).subscribe((dates) => {
      this.showTooltip(`${dates[0]} - ${dates[1]}`);
    });
  }

(formatDate връща Наблюдаем низ)

в теста:

fit('should call mouse enter', () => {
    directive.onMouseenter();
    expect(directive.showTooltip).toHaveBeenCalled();
  });

трябва ли да се подигравам с тръба по някакъв начин? Благодаря!


person nauris-m    schedule 05.03.2020    source източник
comment
Какво връща dateService.formatDate?   -  person Andrew Allen    schedule 05.03.2020
comment
връща Observable‹string›   -  person nauris-m    schedule 05.03.2020
comment
Ясно е, че dateService.formatDate не връща това, което очаквате. Настроили ли сте макет и връщана стойност?   -  person Podge    schedule 05.03.2020
comment
Тези потребители са прави, най-вероятно не се подигравате на dateService правилно. Ако предоставите пълния си код за тестов пакет, мога да ви помогна да го осмиете.   -  person AliF50    schedule 05.03.2020
comment
Публикувах как изглежда теста   -  person nauris-m    schedule 05.03.2020


Отговори (1)


тестът изглежда така

fdescribe('TooltipDirective', () => {
  let directive: TooltipDirective;
  let mockDateService: jasmine.SpyObj<DateService>;

  beforeEach(() => {
    mockDateService = jasmine.createSpyObj('dateService', ['formatDate']);
    directive = new TooltipDirective(null, null, mockDateService);
    directive.tooltipData = {dateStart: new Date(), endDate: Date()};

    spyOn(directive, 'showTooltip');
  });

  fit('should show tooltip', () => {
    directive.onMouseenter();
    expect(directive.showTooltip).toHaveBeenCalled();
  });
});
person nauris-m    schedule 05.03.2020