![](/img/trans.png)
[英]How to pass test-case forcibly in Async await Node.js Unit Testing code using Chai and mocha
[英]Async await Node.js Unit Testing code using Chai and mocha
我對節點和快遞很陌生。 並且一直在嘗試用mocha、chai編寫測試代碼。 這是源代碼的一部分。
googleService.js:
const axios = require('./axios');
exports.hitGoogle = async (requestUrl, payload) => {
let response = {};
await axios.post(`${requestUrl}`, payload)
.then((resp) => {
response = resp;
console.log(response);
})
.catch((err) => {
console.error(err);
});
return response;
};
閱讀Google.js
const googleService = require('./googleService');
exports.execute = async (data) => {
console.log(`data ${JSON.stringify(data)}`);
console.log(`Name ${data.name}`);
console.log(`Salary ${data.salary}`);
console.log(`Age ${data.age}`);
const payload = { name: data.name, salary: data.salary, age: data.age };
await googleService.hitGoogle('http://dummy.restapiexample.com/api/v1/create', payload)
.then((response) => {
if (response.status === 200 || response.status === 201) {
console.log(response.status);
return true;
}
return false;
})
.catch((err) => {
console.log(err);
});
return true;
};
這是我的單元測試文件:
const readGoogle = require('./readGoogle');
const jmsPayload = { age: '23', name: 'test', salary: '123' };
describe('Google ', () => {
it('POST: Google.', async () => {
readGoogle.execute(jmsPayload).then((result) => {
results = result;
console.log(`Final Result : ${results.toString()}`);
});
});
當我執行這個測試文件時,發布成功,
hitGoogle url=http://dummy.restapiexample.com/api/v1/create
hitGoogle payload=[object Object]
{ status: 200,
statusText: 'OK',
從測試 class 傳遞到 readGoogle.js 的輸入值也在其中讀取,
data {"age":"23","name":"test","salary":"123"}
Name test
Salary 123
Age 23
**But I am getting Promise as output.**
**SO I have rendered the promised value to a normal string and getting 'true' as final output.**
還,
1)這是測試異步等待模塊的正確方法嗎?
2) 我們是否需要在此測試代碼中添加任何其他內容,例如 Settimeout 或 beforeEach 或 afterEach 塊?
3) 我們是否需要使用 Sinon 來監視或添加存根到 googleService.js 中的“有效負載”?
任何人都可以幫助或建議我正確的道路嗎?
嘗試了按如下順序一一執行測試用例的異步方式,
const readGoogle = require('./readGoogle');
const jmsPayload = { age: '23', name: 'test', salary: '123' };
const common = require('./common');
const chaiHttp = common.chaiHttp;
common.chai.use(chaiHttp);
describe('Google ', () => {
common.mocha.before((done) => {
// Wait for the server to start up
setTimeout(() => {
done();
}, 1000);
});
it('POST: Google.', async () => {
const result = readGoogle.execute(jmsPayload);
console.log(`Final Result : ${result.toString()}`);
});
it('POST: Google.', async () => {
const result = readGoogle.execute(jmsPayload);
console.log(`Final Result : ${result.toString()}`);
setTimeout(() => {
}, 1000);
});
it('POST: Google.', async () => {
const result = readGoogle.execute(jmsPayload);
console.log(`Final Result : ${result.toString()}`);
});
it('POST: Google.', async () => {
const result = readGoogle.execute(jmsPayload);
console.log(`Final Result : ${result.toString()}`);
});
it('POST: Google.', async () => {
const result = readGoogle.execute(jmsPayload);
console.log(`Final Result : ${result.toString()}`);
});
it('POST: Google.', async () => {
const result = readGoogle.execute(jmsPayload);
console.log(`Final Result : ${result.toString()}`);
});
it('POST: Google.', async () => {
const result = readGoogle.execute(jmsPayload);
console.log(`Final Result : ${result.toString()}`);
});
});
但后來我得到,
錯誤:超過 2000 毫秒的超時。 對於異步測試和鈎子,確保調用了“done()”; 如果返回 Promise,請確保它解析
提前致謝
您可以使用以下 2 個解決方案:
1)回調約定:
describe('Google ', () => {
it('POST: Google.', (done) => {
readGoogle.execute(jmsPayload).then((result) => {
results = result;
console.log(`Final Result : ${results.toString()}`);
done();
}).catch((e) => {
done();
});
});
});
2)異步/等待約定
describe('Google ', () => {
it('POST: Google.', async () => {
const results = await readGoogle.execute(jmsPayload);
console.log(`Final Result : ${results.toString()}`);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.