簡體   English   中英

使用Jasmine測試異步功能的錯誤處理

[英]Test error handling of async function with jasmine

我有以下異步功能:

async $onInit() {
    try {
        this.settings = await this.Service.getSettings(this.companyId);
    } catch (error) {
        this.uiFeedback.showSystemError('There was an error processing the request.', error);
    }
}

我正在嘗試測試如果異步函數捕獲到錯誤,該函數是否顯示錯誤。 目前,唯一成功完成此操作的規范如下:

it('should show error if api call to get availability settings fails', () => {
    Company.getSettings = () => {
        throw new Error();
    };
    try {
        cmp.$onInit().catch(() => {});
    }
    catch (error) {
        expect(uiFeedback.showSystemError).toHaveBeenCalled();
    }

});

因此,基本上,我需要在測試中添加兩個額外的catch塊才能使此測試正常工作。 否則我得到以下錯誤:

ERROR: 'Unhandled promise rejection', 'error'

有一個更好的方法嗎?

因此事實證明,問題僅在於業力如何處理異步功能的錯誤。 this.uiFeedback是在未定義$onInit功能。 但是,直到我在Chrome中運行測試並檢查那里的控制台后,業力才顯示uiFeedback是未定義的錯誤。 在使用業力茉莉花測試async功能時,每個人都需要注意。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM