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。

相关内容

热门资讯

今日头条!微信老友广东辅助器,... 今日头条!微信老友广东辅助器,微乐福建辅助器,安装教程(原来有开挂辅助器)是一款可以让一直输的玩家,...
每日必看推荐!微信小程序家乡大... 每日必看推荐!微信小程序家乡大二辅助工具,新西游辅助器,透视教程(原来有开挂辅助安装);1、选手技艺...
玩家亲测!广东插件智能插件安装... 玩家亲测!广东插件智能插件安装包免费,wepoker轻量版有透视吗,详细教程(原来有开挂辅助挂);广...
重大通报!哈糖大菠萝开挂,we... 重大通报!哈糖大菠萝开挂,we-poker是什么软件,解密教程(原来有开挂辅助助手);1.哈糖大菠萝...
解密关于!鱼乐达人辅助修改器,... 解密关于!鱼乐达人辅助修改器,下载微乐小程序自建房透视,介绍教程(原来有开挂辅助软件);解密关于!鱼...
重大发现!四川家园游戏辅助,微... 重大发现!四川家园游戏辅助,微乐自建房辅助多少钱一个月,AI教程(原来有开挂辅助器);微乐自建房辅助...
玩家必用!玉海楼茶苑怎么开挂,... 玩家必用!玉海楼茶苑怎么开挂,天酷互娱有辅助工具嘛,黑科技教程(原来有开挂辅助插件);天酷互娱有辅助...
一分钟教你!椰岛常胜游戏破解,... 一分钟教你!椰岛常胜游戏破解,广东雀神透视,解密教程(原来有开挂辅助下载);椰岛常胜游戏破解软件透明...
实测发现!三江互娱辅助,wpk... 实测发现!三江互娱辅助,wpk显示有透视挂,详细教程(原来有开挂辅助助手),三江互娱辅助是用手机号来...
程序员教你!长春科乐第三方辅助... 程序员教你!长春科乐第三方辅助工具,wepoker代打辅助机器人,曝光教程(原来有开挂辅助黑科技);...