How can i coverage a promise response with Jasmine and Karma

狂风中的少年 提交于 2019-12-25 01:12:10

问题


I have a function that returns and treats a promise, I need to cover the return that is inside then but I don't know how I can do this, I'm currently trying as follows:

    confirmRemoveUser(user: IUser) {
    this.modalService
        .open('Confirma a exclusão do usuário selecionado?', {
            titleText: 'Confirmando exclusão',
            confirmButtonText: 'Sim',
            cancelButtonText: 'Cancelar',
            closeButtonText: 'Fechar',
            buttonType: 'danger'
        })
        .result.then(
            (result: BentoModalConfirmationCloseReason) => {
                if (result === BentoModalConfirmationCloseReason.Confirm) {
                    if (this.removeUser(user)) {
                        this.toastService.open('Usuário excluído com sucesso!', { type: 'success', close: true });
                    } else {
                        this.toastService.open('Falha ao excluir o usuário!', { type: 'warning', close: true, duration: 0 });
                    }
                }
            }
        );
}

I'm currently using callthrough () and imagine that with some parameter I can get the promise but I don't know how:

   it('Given_ConfirmRemoveUser_When_UserStepIsCalled_Then_UserIsRemoved', (done) => {

        component.selectedJob = {
        };

        component.selectedArea = {
        };

        component.users = [{
        }];

        spyOn(modalService, 'open').withArgs('This is modal msg').and.callThrough();

        component.confirmRemoveUser(component.users[0]);

        expect(modalService.open).toHaveBeenCalled();
        done();
    });

And my coverage is like the image below:

Image here!

UPDATE

New Error


回答1:


Your test should work when it is rewritten as follows:

it('Given_ConfirmRemoveUser_When_UserStepIsCalled_Then_UserIsRemoved', (done) => {
    spyOn(modalService, 'open').and.returnValue(Promise.resolve(BentoModalConfirmationCloseReason.Confirm));
    spyOn(toastService, 'open').and.stub();

    component.confirmRemoveUser(component.users[0])
      .then(r => {
        expect(toastService.open).toHaveBeenCalled();
        done();
      })
      .catch(e => fail(e));
});

You probably also want to know what will be displayed in the toast. Therefore it makes sense to rather use expect(toastService.open).toHaveBeenCalledWith(?);.

UPDATE Above solution only works if confirmRemoveUser would return a Promise.

confirmRemoveUser(user: IUser) {
    return this.modalService
    ...

In your case, the use of the done function does not make sense. You need to use async and await.

it('Given_ConfirmRemoveUser_When_UserStepIsCalled_Then_UserIsRemoved', async () => {
    spyOn(modalService, 'open').and.returnValue(Promise.resolve(BentoModalConfirmationCloseReason.Confirm));
    spyOn(toastService, 'open').and.stub();

    await component.confirmRemoveUser(component.users[0]);

    expect(toastService.open).toHaveBeenCalled();
});

The same can be achieved with fakeAsync and flush.

import { fakeAsync, flush } from '@angular/core/testing';
...
it('Given_ConfirmRemoveUser_When_UserStepIsCalled_Then_UserIsRemoved', fakeAsync(() => {
    spyOn(modalService, 'open').and.returnValue(Promise.resolve(BentoModalConfirmationCloseReason.Confirm));
    spyOn(toastService, 'open').and.stub();

    component.confirmRemoveUser(component.users[0]);
    flush();

    expect(toastService.open).toHaveBeenCalled();
}));


来源:https://stackoverflow.com/questions/59306897/how-can-i-coverage-a-promise-response-with-jasmine-and-karma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!