toHaveBeenCalled() 在模拟输出显示正确数据时失败
toHaveBeenCalled() failing when mocked output shows correct data
我已经厌倦了尝试弄清楚以下内容。基本上我的组件中有一个方法调用一个被模拟的服务。一旦该服务完成,另一个服务就会执行一些日志记录活动,这也是模拟的。但是我的测试失败了,说日志服务没有被调用:
process(){
const that : any = this;
this.mainService.process().then(result=>{
return result;
}).then(result=>{
//log the operation now after doing some checkups
let checkups = ""
that.logService.log('process',result, checkups).then(logged=>{
console.log(logged)
}).catch(err=>console.log(err)
}).catch(err=>console.log(err);
}
在我们继续之前,由于执行检查的逻辑,Promise.all() 与选项不匹配。现在到测试位:
fit("should log processed request", done => {
const mainSerivce = TestBed.get(MainService)
const logService = TestBed.get(LogService)
spyOn(mainService, "process").and.returnValue(Promise.resolve({id:34,value:64, rank:310));
const logSpy = spyOn(logSerivce, "log").and.returnValue(Promise.resolve({'done':true}))
fixture.whenStable().then(finished=>{
component.process();
expect(logSpy).toHaveBeenCalled();
done();
})
});
expect(logSpy).toHaveBeenCalled();
现在失败了,但我可以在我的控制台中看到模拟 {'done':true} 的结果或显示我传递的任何值,这意味着它被模拟并调用了 (?)。我到底错过了什么,因为这些方法似乎已被模拟并正确记录在控制台中。
在我看来你必须等待 promise 解决后再断言,试试:
fit("should log processed request", done => {
const mainSerivce = TestBed.get(MainService)
const logService = TestBed.get(LogService)
spyOn(mainService, "process").and.returnValue(Promise.resolve({id:34,value:64, rank:310));
const logSpy = spyOn(logSerivce, "log").and.returnValue(Promise.resolve({'done':true}));
// call the function that will resolve promises
component.process();
// whenStable waits for the promises to resolve.
fixture.whenStable().then(finished=>{
console.log('Making the assertion !!');
expect(logSpy).toHaveBeenCalled();
done();
});
});
确保您在 Making the assertion !!
的日志之前看到 { 'done': true }
的日志。但是由于您在承诺中解决了承诺,因此以下内容可能会解决它。
fit("should log processed request", async done => { // check out the async keyword here
const mainSerivce = TestBed.get(MainService)
const logService = TestBed.get(LogService)
spyOn(mainService, "process").and.returnValue(Promise.resolve({id:34,value:64, rank:310));
const logSpy = spyOn(logSerivce, "log").and.returnValue(Promise.resolve({'done':true}));
// call the function that will resolve promises
component.process();
// when stable waits for the promises to resolve.
await fixture.whenStable();
await fixture.whenStable();
expect(logSpy).toHaveBeenCalled();
done();
});
我已经厌倦了尝试弄清楚以下内容。基本上我的组件中有一个方法调用一个被模拟的服务。一旦该服务完成,另一个服务就会执行一些日志记录活动,这也是模拟的。但是我的测试失败了,说日志服务没有被调用:
process(){
const that : any = this;
this.mainService.process().then(result=>{
return result;
}).then(result=>{
//log the operation now after doing some checkups
let checkups = ""
that.logService.log('process',result, checkups).then(logged=>{
console.log(logged)
}).catch(err=>console.log(err)
}).catch(err=>console.log(err);
}
在我们继续之前,由于执行检查的逻辑,Promise.all() 与选项不匹配。现在到测试位:
fit("should log processed request", done => {
const mainSerivce = TestBed.get(MainService)
const logService = TestBed.get(LogService)
spyOn(mainService, "process").and.returnValue(Promise.resolve({id:34,value:64, rank:310));
const logSpy = spyOn(logSerivce, "log").and.returnValue(Promise.resolve({'done':true}))
fixture.whenStable().then(finished=>{
component.process();
expect(logSpy).toHaveBeenCalled();
done();
})
});
expect(logSpy).toHaveBeenCalled();
现在失败了,但我可以在我的控制台中看到模拟 {'done':true} 的结果或显示我传递的任何值,这意味着它被模拟并调用了 (?)。我到底错过了什么,因为这些方法似乎已被模拟并正确记录在控制台中。
在我看来你必须等待 promise 解决后再断言,试试:
fit("should log processed request", done => {
const mainSerivce = TestBed.get(MainService)
const logService = TestBed.get(LogService)
spyOn(mainService, "process").and.returnValue(Promise.resolve({id:34,value:64, rank:310));
const logSpy = spyOn(logSerivce, "log").and.returnValue(Promise.resolve({'done':true}));
// call the function that will resolve promises
component.process();
// whenStable waits for the promises to resolve.
fixture.whenStable().then(finished=>{
console.log('Making the assertion !!');
expect(logSpy).toHaveBeenCalled();
done();
});
});
确保您在 Making the assertion !!
的日志之前看到 { 'done': true }
的日志。但是由于您在承诺中解决了承诺,因此以下内容可能会解决它。
fit("should log processed request", async done => { // check out the async keyword here
const mainSerivce = TestBed.get(MainService)
const logService = TestBed.get(LogService)
spyOn(mainService, "process").and.returnValue(Promise.resolve({id:34,value:64, rank:310));
const logSpy = spyOn(logSerivce, "log").and.returnValue(Promise.resolve({'done':true}));
// call the function that will resolve promises
component.process();
// when stable waits for the promises to resolve.
await fixture.whenStable();
await fixture.whenStable();
expect(logSpy).toHaveBeenCalled();
done();
});