Javascript 使用jasmine/karma、spyOn进行角度分量测试

Javascript 使用jasmine/karma、spyOn进行角度分量测试,javascript,angular,typescript,testing,jasmine,Javascript,Angular,Typescript,Testing,Jasmine,我尝试测试我的组件,我知道该组件工作正常,但我的测试给出了错误消息 我写了这个jasmine/karma测试,它抛出了一个我无法通过的错误消息 下面是我要测试的代码: 导出类AddressListComponent实现OnInit{ paginate:PaginateInterface=新的paginate(地址); 地址:AddressInterface[]=[]; 建造师( 专用地址服务:地址服务, 专用addressTypeService:addressTypeService, 私人cou

我尝试测试我的组件,我知道该组件工作正常,但我的测试给出了错误消息 我写了这个jasmine/karma测试,它抛出了一个我无法通过的错误消息

下面是我要测试的代码:

导出类AddressListComponent实现OnInit{
paginate:PaginateInterface=新的paginate(地址);
地址:AddressInterface[]=[];
建造师(
专用地址服务:地址服务,
专用addressTypeService:addressTypeService,
私人countryService:countryService,
私人结算服务:结算服务,
站点的私有服务性质:站点服务的性质,
专用路由器:路由器,
) {
}
恩戈尼尼特(){
如果(!this.isBind){
this.addressService.getAll().subscribe((解析:PaginateInterface)=>{
this.paginate=解析;
this.addresses=this.paginate.data;
},
错误=>{
抛出(错误);
});
}
}
}
下面是测试:

description('AddressListComponent',()=>{
const httpClient=新的httpClient(null);
let注射器:注射器;
let组件:AddressListComponent;
let服务:地址服务;
让结算服务:结算服务;
让countryService:countryService;
让addressTypeService:addressTypeService;
让站点服务的性质:站点服务的性质;
常数数据=[
{name:'测试1'},
{name:'测试2'},
{name:'测试3'},
];
常量分页数据=新分页(地址{
数据:数据,
});
在每个之前(()=>{
沉降服务=新沉降服务(httpClient,注射器);
countryService=新的countryService(httpClient、injector);
addressTypeService=新的addressTypeService(httpClient,injector);
站点服务的性质=站点服务的新性质(httpClient,injector);
服务=新地址服务(httpClient、injector、settlementService、countryService、addressTypeService、SiteService的性质);
});
它('应该为从服务器返回的项目设置address属性',()=>{
spyOn(服务,'getAll')。和.returnValue(分页数据的));
组件=新的AddressListComponent(服务、addressTypeService、countryService、结算服务、站点服务的性质,null);
expect(component.addresses.length).toBe(datas.length);
});
});
控制台将返回此错误消息:

    Chrome 79.0.3945 (Windows 10.0.0) AddressListComponent should set address property with the items returned from server FAILED
            Expected 0 to be 3.
                at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/components/address/address-list/address-list.component.spec.ts:49:40)
    Chrome 79.0.3945 (Windows 10.0.0): Executed 1 of 2 (1 FAILED) (0 secs / 0.137 secs)
    Chrome 79.0.3945 (Windows 10.0.0) AddressListComponent should set address property with the items returned from server FAILED
            Expected 0 to be 3.
                at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/components/address/address-list/adChrome 79.0.3945 (Windows 10.0.0): Executed 2 of 2 (1 FAILED) (0.146 secs / 0.138 secs)
    TOTAL: 1 FAILED, 1 SUCCESS
    TOTAL: 1 FAILED, 1 SUCCESS
Chrome 79.0.3945(Windows 10.0.0)AddressListComponent应设置address属性,但从服务器返回的项目失败 预期0为3。 在UserContext。(http://localhost:9876/_karma_webpack_/src/app/components/address/address-列表/地址列表。组件规范ts:49:40) Chrome 79.0.3945(Windows 10.0.0):执行2次中的1次(1次失败)(0秒/0.137秒) Chrome 79.0.3945(Windows 10.0.0)AddressListComponent应该为从服务器返回的项目设置address属性失败 预期0为3。 在UserContext。(http://localhost:9876/_karma_webpack_/src/app/components/address/address-list/adChrome 79.0.3945(Windows 10.0.0):执行2次,共2次(1次失败)(0.146秒/0.138秒) 总计:1次失败,1次成功 总计:1次失败,1次成功
您知道我的测试出了什么问题吗?(我知道代码工作正常,但测试失败)

似乎您需要在测试用例中显式调用ngOnInit(),以便初始化组件:

 it('should set address property with the items returned from server', () => {
    spyOn(service, 'getAll').and.returnValue(of(paginateData));

    component = new AddressListComponent(service, addressTypeService, countryService, settlementService, natureOfTheSiteService, null);
    component.ngOnInit();
    expect(component.addresses.length).toBe(datas.length);
  });

如果您正在使用TestBed创建组件及其依赖项,如官方文档中所述,您可以调用detectChanges方法来初始化组件(它调用ngOnInit)

我强烈建议您阅读。ngOnInit从未被调用过,但您的测试方法还有其他问题。请给我一些建议,或者给我一个代码,我如何才能正确地测试它@jornsharpei建议您更仔细地阅读文档,但最重要的是本测试床部分。