【发布时间】:2018-06-04 10:38:29
【问题描述】:
我需要测试这个功能。
当我点击这个功能getallproductcomponent()时,执行这个服务功能getallproductservice()并返回所有产品。请问有什么办法,如何测试?
getallproductcomponent() {
this.ws.getallproductservice().subscribe(
item=> {
this.item= item;
}
);
}
只有getallproductservice我像这段代码一样测试,但是如何测试组件。
it('testing',
async(inject([ProductService], (service: ProductService) => {
TestBed.get(MockBackend).connections.subscribe(
(connection: MockConnection) => connection.mockRespond(new Response(
new ResponseOptions({
})
))
);
service.getallproductservice().subscribe(items => {
expect(items[0].alarmdesc).toEqual('call');
});
})))
【问题讨论】:
-
您必须模拟/存根
ws服务,它是getallproductservice方法。阅读Angular official documentaion 中的这篇文章,该文章描述了如何测试具有依赖关系的组件 -
是的,我知道,但是如何在组件中实现,以及当我有一个调用内部http服务的函数时?
标签: angular unit-testing testing jasmine karma-runner