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、许多玩家不知...
教程辅助!手游蛮王辅助,崇阳斗... 教程辅助!手游蛮王辅助,崇阳斗棋bug,详细插件(有挂规律)1、手游蛮王辅助辅助器安装包、手游蛮王辅...
黑科技教程!约局吧德州透视,德... 黑科技教程!约局吧德州透视,德扑圈有透视吗,一贯真的是有挂(真是有挂)一、德扑圈有透视吗可以开透视的...
盘点几款!科乐辅助工作室,透视... 盘点几款!科乐辅助工作室,透视盒子,详细技巧(真的有挂)进入游戏-大厅左侧-新手福利-激活码辅助透视...
连日来!aapoker辅助怎么... 连日来!aapoker辅助怎么用,德扑圈透视,其实是有挂(有挂教学)1、金币登录送、破产送、升级送、...
一分钟揭秘!四川途游小程序辅助... 一分钟揭秘!四川途游小程序辅助,雀神广东插件智能解码,详细app(有挂总结)1、很好的工具软件,可以...
最新消息!wepoker有辅助... 最新消息!wepoker有辅助吗,德扑之心免费透视,原来真的有挂(有挂方法)1、玩家可以在德扑之心免...
总算了解!福建兄弟十三水挂件去... 总算了解!福建兄弟十三水挂件去那买,凑一桌小程序辅助器,详细脚本(有挂方法)1)福建兄弟十三水挂件去...
黑科技辅助挂!wepoker俱... 您好,德扑之星有辅助软件吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...
我来教大家!途游手游辅助,小闲... 我来教大家!途游手游辅助,小闲川南怎么辅助,详细脚本(有挂总结)1、超多福利:超高返利,海量正版游戏...