【问题标题】:Cannot set properties of null (setting 'value'). Angular/Jasmine Issue with component template tests无法设置 null 的属性(设置“值”)。组件模板测试的 Angular/Jasmine 问题
【发布时间】:2022-01-07 12:47:04
【问题描述】:

我有一个组件的下一个测试:

带有输出的测试响应:“无法设置 null 属性(设置‘值’)” 但有时它会起作用。问题是当测试尝试设置值时“memberMessage”为空。有什么办法可以解决?

【问题讨论】:

    标签: angular unit-testing jasmine


    【解决方案1】:

    下次尽量不要贴图片,自己贴代码。

    这不是一个完整的答案,但它应该可以帮助您调试

     it('changes to last value if invalid', async () => {
       // wait for promises to complete - (1)
       await fixture.whenStable();
       // call fixture.detectChanges() again after promises - (2)
       fixture.detectChanges()
       const memberMessage = fixture.debugElement.nativeElement.querySelector('#mat-input-2');
       console.log({ memberMessage }); // ensure this is not null
     });
    

    如果上述方法不起作用,请尝试使用来自herewaitUntil 实用程序。

    it('changes to last value if invalid', async () => {
      // it will waitUntil the below function returns true
      await waitUntil(() => !!fixture.debugElement.nativeElement.querySelector('#mat-input-2'));
      const memberMessage = fixture.debugElement.nativeElement.querySelector('#mat-input-2');
      console.log({ memberMessage }); // should hopefully not be null now
    });
    

    【讨论】:

      猜你喜欢
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多