我如何用茉莉花和因果报应诺言 [英] How can i coverage a promise response with Jasmine and Karma
问题描述
我有一个返回并处理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
函数是没有意义的.您需要使用async
和await
.
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();
});
使用fakeAsync
和flush
可以实现相同的目的.
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屋!