1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Angular 2 Unit Test for a promise

Discussão em 'Angular' iniciado por AngularM, Outubro 10, 2024 às 12:13.

  1. AngularM

    AngularM Guest

    I normally use fakeAsync to test a subscribe returning an observable. But on this rare occasion, I need to do the same but for a promise.

    This is my attempt:

    //Service Stub:

    const testService = {
    testMethod:
    jasmine.createSpy('testMethod').and.callFake(() => new Promise(() => 'test'))
    };

    it('test example',() => {
    // Arrange
    const response = 'test';
    component.selected = undefined;

    // Act
    component['myMethod']();

    //Assert
    expect(component.selected).toEqual(response);
    });


    This is my actual code:

    private myMethod(): void {
    return this.testService.testMethod()
    .then((response: string) => {
    this.selected = response;
    })
    .catch(error => this.logger.error(error, this));
    }


    Basically, need to know how to wait for "this.testService.testMethod" to return and set selected.

    Currently, it's not waiting for the promise to return.

    Note: I've updated my current attempt. But still got undefined in the expect.

    Continue reading...

Compartilhe esta Página