[英]How to pass test-case forcibly in Async await Node.js Unit Testing code using Chai and mocha
我正在使用下面的測試用例來測試 Mocha 和 Chai 中的服務,它工作正常。
describe('Google ', () => {
it('POST: Google.', async () => {
const results = await readGoogle.execute(jmsPayload);
console.log(`Final Result : ${results.toString()}`);
});
});
對於上述代碼,我需要處理一種情況。 實際上,我有時會從服務 class readGoogle.execute 方法中得到這個異常。
TypeError: Cannot read property 'status' of undefined
這是從 readGoogle.execute 方法的角度來看的。 但我的要求是我需要通過上述測試用例,即使我從 readGoogle.execute await 方法中得到錯誤。
1)我無權訪問 readGoogle.execute 方法,所以我無法處理那里的未定義檢查。 僅在我的測試用例中要做的任何事情。
2)我試圖在上面'it'
中return true,
但測試用例仍然失敗。
3)我也試過了,斷言(真); 在it
,但測試用例仍然失敗。
任何人都可以向我建議一些我可以始終通過上述 test_case 的想法(即使在成功和失敗的情況下)?
提前致謝。
據我了解,您對上面的readGoogle.execute調用進行了測試,並且由於它是外部資源/api,因此有時會出現異常,這是正確的行為。
在這種情況下,我建議將此調用包裝在 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 中做出一些其他斷言,以確保這正是您預期的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.