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辅助器怎么弄(AApoker透视)原来是真的有辅助工具(哔哩哔哩)wepok...
透视教材!哈糖大菠萝有挂吗(透... 透视教材!哈糖大菠萝有挂吗(透视)newpoker怎么安装脚本(辅助)真是真的是有教程(哔哩哔哩)1...
透视经验!pokemmo免费脚... 透视经验!pokemmo免费脚本(透视)xpoker辅助(辅助)真是是有神器(哔哩哔哩)1、这是跨平...
透视秘籍!红龙poker辅助(... 透视秘籍!红龙poker辅助(透视)wepoker透视有吗(辅助)竟然是有工具(哔哩哔哩)1、wep...
透视总结!wejoker辅助器... 透视总结!wejoker辅助器怎么卖(HHpoker买挂)都是真的有辅助教程(哔哩哔哩)1.wejo...
透视总结!智星德州插件最新版本... 透视总结!智星德州插件最新版本更新内容详解(透视)约局吧透视挂下载(辅助)果然一直都是有插件(哔哩哔...
透视步骤!pokerrrr2辅... 透视步骤!pokerrrr2辅助(透视)德州辅助工具到底怎么样(辅助)真是有软件(哔哩哔哩)1、完成...
透视模板!wepoker黑侠辅... 透视模板!wepoker黑侠辅助器(HHpoker插件)原来是有辅助插件(哔哩哔哩)1、操作简单,无...
透视绝活儿!hhpoker真的... 透视绝活儿!hhpoker真的有透视吗(WPK官网)总是真的有辅助脚本(哔哩哔哩)1、金币登录送、破...
透视手册!约局吧如何查看是否有... 透视手册!约局吧如何查看是否有挂(透视)werplan透视挂(辅助)一直有插件(哔哩哔哩)1、进入到...