Angular单元测试ComponentFixture的实例化过程明细

时间:2022-07-26
本文章向大家介绍Angular单元测试ComponentFixture的实例化过程明细,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

从angular core里导入单元测试需要的库文件:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

定义一个fixture:

let fixture: ComponentFixture;

fixture的实例化方法:

  beforeEach(() => {
    fixture = TestBed.createComponent(ProductCarouselComponent);
    fixture.detectChanges();
  });

观察其实例化过程:

使用testBedRender3创建Component:

type就是ProductCarouselComponent的构造函数:

使用Componentfactory创建实例:

拿到Component selector的值:

最后创建好的fixture如下:

其Componentinstance属性存放的就是ProductCarouselComponent的实例: