繁体   English   中英

如何使用spyOn测试Promise.all的功能

[英]How to test then function of Promise.all with spyOn

我是编码的新手,因此请询问是否需要更多信息。

我想用spyOn测试Promise.all内部的一个then块,但是永远不会调用该函数。

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块不被调用。

表示异步操作最终完成或失败的承诺。 在您的测试,检查是否在controller.ok被调用, Promise返回由Promise.all方法foo尚未解决。 因此,您需要某种同步。

一种可能的解决方案如下所示。

it('should call controller.ok when name is set', () => {
    const promises: Promise<any>[] = [];
    spyOn(nameService, 'doSomething').and.callFake(n => {
        const promise = Promise.resolve();
        promises.push(promise);
        return promise;
    });
    spyOn(controller, 'ok');

    service.foo();

    Promise.all(promises)
         .then(r => expect(controller.ok).toHaveBeenCalled());
});

通过使用fakeAsync@angular/core/testing tick可以实现相同的目的。

it('should call controller.ok when name is set', fakeAsync(() => {
    spyOn(nameService, 'doSomething').and.returnValue(Promise.resolve());
    spyOn(controller, 'ok');

    service.foo();
    tick();

    expect(controller.ok).toHaveBeenCalled();
}));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM