【发布时间】:2019-08-08 09:40:54
【问题描述】:
我是编码新手,所以请询问是否需要更多信息。
我想用 spyOn 测试 Promise.all 中的 then-block,但从未调用过该函数。
public foo(): void {
const names = this.getNames();
Promise.all(
names.map(name =>
this.nameService.doSomething( //some params )
)
)
.then(result => this.controller.ok(names))
.catch(error => {
//do something
});
}
这是测试
it('should call controller.ok when name is set', () => {
spyOn(nameService, 'doSomething').and.returnValue(Promise.resolve());
spyOn(controller, 'ok');
service.foo();
expect(nameService.doSomething).toHaveBeenCalledWith({
//some params
});
expect(controller.ok).toHaveBeenCalled(); //fails because never called
});
我已经调试了代码,即使使用正确的参数也调用了 doSomething,代码也到达了 then 块。 但是测试说,它永远不会被调用,所以那里的代码中断了,我不知道为什么?
catch-block 没有被调用。
【问题讨论】:
标签: javascript testing jasmine