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。

相关内容

热门资讯

玩家必用!pokemmo辅助脚... 玩家必用!pokemmo辅助脚本,wpk俱乐部是真的吗,竟然真的有挂(有挂实锤)1、完成有辅助插件,...
分辨真假!wepoker有脚本... 分辨真假!wepoker有脚本吗,拱趴大菠萝开挂方法,一贯真的是有挂(存在有挂);1、用户打开应用后...
实测发现!约局吧游戏挂,wej... 实测发现!约局吧游戏挂,wejoker辅助软件,一贯有挂(有挂教学)1、破解器简单,机器人操作,插件...
玩家必看教程!hhpoker辅... 玩家必看教程!hhpoker辅助器视频,wepoker怎么增加运气,其实真的是有挂(真是有挂)1、辅...
程序员教你!werplan外卦... 程序员教你!werplan外卦神器,hhpoker外挂靠谱吗,果然真的有挂(有挂讲解)1、在插件功能...
玩家必看科普!we-poker... 玩家必看科普!we-poker辅助,wepoker轻量版有透视吗,一直存在有挂(有挂方略)1、首先打...
玩家攻略!wepoker亲友圈... 玩家攻略!wepoker亲友圈有用吗,aapoker怎么控制牌,其实存在有挂(有挂技巧)1、点击下载...
一分钟了解!fishpoker... 一分钟了解!fishpoker透视,htx矩阵wepoker辅助,切实是有挂(确实有挂)1、打开软件...
记者发布!约局吧德州透视,cl... 记者发布!约局吧德州透视,cloudpoker外挂,好像是真的有挂(有挂秘诀)1)有没有挂:进一步探...
如何分辨真伪!悟空黑桃a有挂嘛... 如何分辨真伪!悟空黑桃a有挂嘛,wepokerplus开挂,好像真的是有挂(有挂秘笈)1)辅助插件:...