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。

相关内容

热门资讯

查到实测辅助!pokermas... 查到实测辅助!pokermaster修改器,wepoker透视器免费(力荐透视开挂辅助工具)是一款可...
一分钟了解!poker wor... 您好:poker world辅助器这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现...
攻略讲解!pokemmo脚本辅... 攻略讲解!pokemmo脚本辅助,如何判断wpk辅助软件的真假(线上透视开挂辅助挂)是一款可以让一直...
玩家必用!wpk安卓下载辅助,... 玩家必用!wpk安卓下载辅助,德普软件(实用透视开挂辅助插件)是一款可以让一直输的玩家,快速成为一个...
普及知识!wepoker可以破... 普及知识!wepoker可以破解游戏,wepoker透视app下载(存在挂透视开挂辅助软件)是一款可...
总算了解!约局吧德州透视,we... 您好:wepoker软件安装包这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
避坑细节!wepoker免费辅... 避坑细节!wepoker免费辅助器,wepoker好友局透视(解说透视开挂辅助神器)是一款可以让一直...
专业讨论!悦扑克脚本,wepo... 您好:wepoker透视底牌脚本这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...
教学盘点!wpk透视是真的,约... 教学盘点!wpk透视是真的,约局吧德州有挂(总结透视开挂辅助脚本);教学盘点!wpk透视是真的,约局...
玩家必备攻略!wpk私人局有透... 玩家必备攻略!wpk私人局有透视,wepoker辅助透视软件(安装透视开挂辅助软件)是一款可以让一直...