Angular的fixture的debugElement为NULL。
创始人
2024-10-24 03:00:58
0

在Angular中,debugElement为NULL通常是因为fixture还没有完成初始化。解决这个问题的方法是使用async/await或者使用fixture的detectChanges方法来确保fixture的初始化完成。

下面是一个使用async/await的示例代码:

import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { MyComponent } from './my.component';

describe('MyComponent', () => {
  let component: MyComponent;
  let fixture: ComponentFixture;

  beforeEach(waitForAsync(() => {
    TestBed.configureTestingModule({
      declarations: [ MyComponent ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
  });

  it('should create', async () => {
    fixture.detectChanges();
    await fixture.whenStable(); // 等待fixture初始化完成
    expect(component).toBeTruthy();
  });
});

在上述代码中,我们使用了async/await来等待fixture初始化完成。在测试方法中,我们首先调用fixture.detectChanges()来触发组件的变化检测,然后使用fixture.whenStable()来等待fixture初始化完成。

另一种解决方法是使用fixture的detectChanges方法:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MyComponent } from './my.component';

describe('MyComponent', () => {
  let component: MyComponent;
  let fixture: ComponentFixture;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [ MyComponent ]
    })
    .compileComponents();

    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
  });

  it('should create', () => {
    fixture.detectChanges();
    fixture.whenStable().then(() => { // 使用whenStable方法来等待fixture初始化完成
      expect(component).toBeTruthy();
    });
  });
});

在上述代码中,我们仍然使用fixture.detectChanges()来触发组件的变化检测,然后使用fixture.whenStable().then()来等待fixture初始化完成,并在回调函数中进行断言。

无论使用哪种方法,都可以确保fixture初始化完成后再访问debugElement。

相关内容

热门资讯

透视代打!wepoker辅助器... 透视代打!wepoker辅助器安装包,素来真的是有挂(透视)技巧教程(有挂工具)1)wepoker辅...
透视存在!哈糖大菠萝软件下载,... 透视存在!哈糖大菠萝软件下载,德州辅助工具到底怎么样,系统教程(有挂辅助);1.德州辅助工具到底怎么...
透视神器!sohoo辅助,从来... 透视神器!sohoo辅助,从来有挂(透视)解密教程(有挂插件)1、进入游戏-大厅左侧-新手福利-激活...
透视规律!约局吧德州真的有透视... 透视规律!约局吧德州真的有透视挂吗,pokermaster修改器,科技教程(有挂解密)1、透视规律!...
透视脚本!来玩德州破解器,切实... 透视脚本!来玩德州破解器,切实真的是有挂(透视)必备教程(有挂解说);1、游戏颠覆性的策略玩法,独创...
透视黑科技!德州私人局脚本,p... 透视黑科技!德州私人局脚本,pokerworld破解版下载,切实教程(有挂教程)1、很好的工具软件,...
透视脚本!wejoker免费脚... 透视脚本!wejoker免费脚本,本然是真的有挂(透视)2025新版教程(有挂详情);1、上手简单,...
透视苹果版!pokeplus脚... 透视苹果版!pokeplus脚本,德州透视插件,实用技巧(有挂黑科技);小薇(透视辅助)致您一封信;...
透视有挂!约局吧开挂神器是真的... 透视有挂!约局吧开挂神器是真的吗,从前真的是有挂(透视)扑克教程(有挂解密)1、约局吧开挂神器是真的...
透视新版!aa poker辅助... 透视新版!aa poker辅助,哈糖大菠萝挂,2025新版总结(有挂解说)1、用户打开应用后不用登录...