jeudi 8 août 2019

How to test then function of Promise.all with spyOn

I'm a newbie to coding, so please ask if more information is necessary.

I want to test a then-block inside a Promise.all with spyOn, but the function get never called.

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
      });
  }


This is the test

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
  });

I've debugged the code and the doSomething get called even with the right params, the code also reaches the then-block. But the test says, it never gets called, so somewhere in there the code breaks and I don't know why?

The catch-block is not called.

Aucun commentaire:

Enregistrer un commentaire