我如何用茉莉花和因果报应诺言 [英] How can i coverage a promise response with Jasmine and Karma

查看:88
本文介绍了我如何用茉莉花和因果报应诺言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回并处理promise的函数,我需要介绍then内部的返回,但是我不知道该怎么做,我目前正在尝试如下操作:

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

我目前正在使用callthrough (),并想象通过一些参数我可以得到承诺,但我不知道如何:

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

我的报道类似于下图:

此处的图片!

新错误

推荐答案

您的测试按以下方式重写时应该可以正常工作:

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

您可能还想知道烤面包中将显示什么.因此,宁可使用expect(toastService.open).toHaveBeenCalledWith(?);.

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

更新 仅当confirmRemoveUser返回Promise时,以上解决方案才有效.

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

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

对于您而言,使用done函数是没有意义的.您需要使用asyncawait.

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

使用fakeAsyncflush可以实现相同的目的.

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

这篇关于我如何用茉莉花和因果报应诺言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆