如何在spec.ts文件(ng test)中模拟/伪造组件实例的HTML代码

时间:2019-06-03 12:00:54

标签: angular typescript mocking

我的spec.ts文件:

describe('CenterPricesComponent', () => {

    let component: CenterPricesComponent;
    let fixture: ComponentFixture<CenterPricesComponent>;
    let service: MockPriceRulesService;
    let centersService: MockCentersService;

    beforeEach(async(() => {
        service = new MockPriceRulesService(null);
        centersService = new MockCentersService(null);
        TestBed.overrideProvider(PriceRulesService, { useValue: service });
        TestBed.overrideProvider(CentersService, { useValue: centersService});
        TestBed.configureTestingModule(testBed).compileComponents();

        fixture = TestBed.createComponent(CenterPricesComponent);
        fixture.detectChanges();
        component = fixture.componentInstance;
    }));
.
.
.

在这里,我要用以前创建的伪造服务来模拟组件中使用的两个服务。我想知道是否可以使用这个componentInstance component的HTML模板来做到这一点。

编辑:

constructor(
    private priceruleService: PriceRulesService,
    private windowService: NbWindowService,
    private centerService: CentersService,
    private route: ActivatedRoute
  ) { }

1 个答案:

答案 0 :(得分:0)

如果您不想使用HTML,那么根本就不要使用测试床,它基本上就是为了使用它。

let component: CenterPricesComponent = new CenterPricesComponent(
  // Your mocked dependencies
);

编辑

如果我不太清楚,则需要删除它

let component: CenterPricesComponent = new CenterPricesComponent(
  // Your activated route
  { snapshot: { paramMap : { get: () => '', } } } as any
);