Проверьте элементы ngIf в жасмине

#angular #jasmine #karma-jasmine #karma-runner

#angular #jasmine #карма-жасмин #карма-бегун

Вопрос:

У меня есть элемент управления, заключенный в ngIf, при запуске ng test элементы внутри ngIf не отображаются в представлении jasmine.

spec.ts

  it('Master card', () => {
    component.getCardType('5453010330100560');
    fixture.detectChanges();
    expect(component.cardType).toBe('MSCD');
  });
 

components.ts

 getCardType(number) {
  //I have a function call here 
    this.getMonths();
    ....
}
 

Получить функцию месяцев

 getMonths() {
    const year = this.ipExpiryYear.nativeElement;
    ....
}
 

Ссылка на элемент ViewChild элемента

 @ViewChild('expiryYear') ipExpiryYear: ElementRef;
 

HTML-файл

  <select #expiryYear  *ngIf="!isMobileView" >
    <option disabled hidden [value]="undefined"></option>
 </select>
 

Я также попытался инициализировать переменную ngIf

   beforeEach(() => {
    fixture = TestBed.createComponent(CardComponent);
    httpMock = TestBed.get(HttpTestingController);
    component = fixture.componentInstance;
    component.isMobileView = false;
    fixture.detectChanges();
  });
 

Когда m running ng test, I я получаю ошибку
Ошибка типа: не удается прочитать свойство ‘nativeElement’ undefined, пожалуйста, помогите решить эту проблему

Комментарии:

1. похоже, что getCardType вызывается до настройки представления

2. Я также попытался настроить beforeEach(() => { fixture = TestBed.createComponent(CardComponent); httpMock = TestBed.get(HttpTestingController); component = fixture.componentInstance; component.isMobileView = false; fixture.DetectChanges(); });

3. @manomanoj: Вы должны использовать весь компонент целиком. это помогло бы нам понять последовательность вызовов функций и рендеринга