【发布时间】:2021-04-02 17:26:27
【问题描述】:
我在 component.ts 文件中有一个方法,它正在调用服务方法。 代码结构如下-
getDetails(){
if(!alreadyCached){
callLogFunction();
}
this.service.getData()
.then(res => {
//rest of the logic here
})
.catch(err=>{ //catch logic here })
}
我的测试块是这样的-
it('should call service.getData', ()=>{
spyOn(service,'getData').and.returnValue(Promise.resolve([]);
component.getDetails();
fixture.whenStable().then(()=>{
expect(service.getData).toHaveBeenCalled();
}
}
为什么我会收到预计会调用 spy getData 的错误 如果你能帮助我进行测试并捕获块(如何为它们编写测试),那就太好了
谢谢! :)
【问题讨论】:
标签: javascript angular unit-testing jasmine karma-jasmine