如何为Node.js express框架应用程序编写mocha和chai的单元测试头代码?



我正在编写测试用例,以检查收到的所有头是否拼写正确假设有多个字段名,我想为它们所有的字段名做测试用例,它们都是正确拼写的,写在对象数组中。那么我如何在mocha和chai中编码

您可以尝试这样的东西,因为不清楚您的用例是什么,这些是我在.to.deep.equalInAnyOrder如果拼写不匹配将抛出错误之前使用的一些测试。你必须创建测试数据,我不完全确定你在寻找什么,但第二种可能更合适。

it('200', async () => {
nock('https://test.ca')
.get('/test')
.reply(200, {
data: 'test',
});
const result = await rest.get('test', 'test');
assert.equal(result.statusCode, 200);
assert.deepEqual(result.body, { data: 'test' });
assert.isTrue(nock.isDone());
});

你可以创建一个包含所有结果的对象在我的例子中是s10

it('Get student details', () => {
return request(app)
.get('/api/v3/student/10')
.expect(httpStatus.OK)
.then(async (res) => {
expect(res.body).to.be.an('object');
expect(res.body.career).to.be.an('array');
expect(res.body.career).to.deep.equalInAnyOrder(s10.career);
});
});