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。

相关内容

热门资讯

我来教大家!红龙poker辅助... 我来教大家!红龙poker辅助挂多功能透视工具,果然有挂(有挂透视)1、任何红龙poker透视是真的...
一分钟揭秘!德扑圈辅助挂多功能... 一分钟揭秘!德扑圈辅助挂多功能透视工具,都是是真的有挂(有挂技术)德扑圈脚本下载中分为三种模型:德扑...
推荐十款!德普之星辅助挂多功能... 推荐十款!德普之星辅助挂多功能透视工具,原来有挂(真是有挂)运德普之星辅助工具,进入游戏界面。进入德...
科技分享!微扑克辅助挂多功能透... 科技分享!微扑克辅助挂多功能透视工具,好像有挂(有人有挂)1、游戏颠覆性的策略玩法,独创攻略技巧玩法...
传递经验!wpk辅助挂多功能透... 传递经验!wpk辅助挂多功能透视工具,确实是真的有挂(有挂透明挂)小薇(辅助器软件下载)致您一封信;...
重大来袭!微扑克辅助挂多功能透... 重大来袭!微扑克辅助挂多功能透视工具,切实真的是有挂(详细教程)1、操作简单,无需微扑克手机版透视脚...
揭秘一下!aapoker辅助挂... 揭秘一下!aapoker辅助挂多功能透视工具,好像有挂(有挂头条)1、aapoker破解器简单,aa...
2024教程!德扑圈辅助挂多功... 2024教程!德扑圈辅助挂多功能透视工具,一贯是有挂(有挂存在)1、操作简单,无需德扑圈手机版透视脚...
推荐十款!微扑克辅助挂多功能透... 推荐十款!微扑克辅助挂多功能透视工具,好像真的是有挂(发现有挂)1、打开软件启动之后找到中间准星的标...
分享一款!wepoker辅助挂... 分享一款!wepoker辅助挂多功能透视工具,好像是有挂(竟然有挂)1、玩家可以在wepoker透视...