Async await Node.js 使用 Chai 和 mocha 的单元测试代码
Async await Node.js Unit Testing code using Chai and mocha
我对 node 和 express 很陌生。并且一直在尝试使用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;
};
readGoogle.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()}`);
});
});
当我执行这个测试文件时,post成功,
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)这是测试async-await模块的正确方法吗?
2) 我们是否需要在此测试代码中添加任何其他内容,例如 Settimeout
或 beforeEach 或 afterEach 块?
3) 我们是否需要使用 Sinon 进行间谍活动或向 'payload' 添加存根
在 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,确保它 resolve
提前致谢
您可以使用以下两种解决方案:
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) async/await 惯例
describe('Google ', () => {
it('POST: Google.', async () => {
const results = await readGoogle.execute(jmsPayload);
console.log(`Final Result : ${results.toString()}`);
});
});
我对 node 和 express 很陌生。并且一直在尝试使用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;
};
readGoogle.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()}`);
});
});
当我执行这个测试文件时,post成功,
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)这是测试async-await模块的正确方法吗?
2) 我们是否需要在此测试代码中添加任何其他内容,例如 Settimeout 或 beforeEach 或 afterEach 块?
3) 我们是否需要使用 Sinon 进行间谍活动或向 'payload' 添加存根 在 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,确保它 resolve
提前致谢
您可以使用以下两种解决方案:
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) async/await 惯例
describe('Google ', () => {
it('POST: Google.', async () => {
const results = await readGoogle.execute(jmsPayload);
console.log(`Final Result : ${results.toString()}`);
});
});