【问题标题】:TypeError: Cannot read property 'nativeElement' of undefined in jasmine unit test caseTypeError:无法读取茉莉花单元测试用例中未定义的属性“nativeElement”
【发布时间】:2020-10-10 07:04:30
【问题描述】:

在我的 TS 文件下面

   @ViewChild('creditName', { read: false, static: false }) public creditName: ElementRef<any>;

onChange() {enter code here this.creditName.nativeElement.style.border = '1px 纯红色'; }

在我的 HTML 中

谁能指导我,如何编写上面的测试用例...

我在下面尝试过,但没有任何效果

  1. spyOnProperty(div.nativeElement, 'style:border', 'get').and.returnValue('1px solid red');
  2. component.creditName = fixture.debugElement.query(By.css('.for-testing'));

【问题讨论】:

    标签: angular jasmine


    【解决方案1】:

    您可能在测试中遗漏了这一点,以便您的组件模板呈现:

    fixture.detectChanges();
    

    如果组件中的变量影响其模板的呈现(例如,在隐藏输入字段的 ngIf 的情况下),也需要在组件中更改变量后调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-06
      • 2021-02-22
      • 2013-11-14
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多