测试茉莉花中的 ngIf 元素

问题描述

我有一个包含在 ngIf 中的控件,在运行 ng 测试时,ngIf 中的元素不会在 jasmine 视图中呈现。

规格

 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”,请帮助解决此问题

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)