qq_遁去的一_1
2022-01-07 19:29:03
我正在使用下面的測試用例來測試 Mocha 和 Chai 中的服務(wù),它工作正常。describe('Google ', () => { it('POST: Google.', async () => { const results = await readGoogle.execute(jmsPayload); console.log(`Final Result : ${results.toString()}`); });});對于上述代碼,我需要處理一種情況。實際上我有時會從服務(wù)類 readGoogle.execute 方法中得到這個異常。 TypeError: Cannot read property 'status' of undefined這是從 readGoogle.execute 方法的角度來看的。但我的要求是我需要通過上述測試用例,即使我從 readGoogle.execute await 方法中得到錯誤。1)我無權(quán)訪問 readGoogle.execute 方法,所以我無法處理那里的未定義檢查。僅在我的測試用例中要做的任何事情。2)我return true,在上面嘗試過,'it'但測試用例仍然失敗。3)我也試過了,斷言(真);在上面it,但測試用例仍然失敗。任何人都可以向我建議一些我可以始終通過上述 test_case 的想法(即使在成功和失敗的情況下)?提前致謝。
1 回答

搖曳的薔薇
TA貢獻1793條經(jīng)驗 獲得超6個贊
據(jù)我了解,您對上面的readGoogle.execute調(diào)用進行了測試,并且由于它是外部資源/api,因此有時會出現(xiàn)異常,這是正確的行為。
在這種情況下,我建議將此調(diào)用包裝在 try-block 中。
describe('Google ', () => {
it('POST: Google.', async () => {
try {
const results = await readGoogle.execute(jmsPayload);
console.log(`Final Result : ${results.toString()}`);
//maybe some other assertion here about result object.
} catch (e) {
assert.equal(e.name, 'TypeError');
}
});
});
或者在 catch 中做出一些其他斷言,以確保這正是您預(yù)期的錯誤。
添加回答
舉報
0/150
提交
取消