如何在JSON对象中发送数组以进行ChaiHttp单元测试? [英] How to send array in JSON object for ChaiHttp Unit Testing?
问题描述
我在node.js中有一个API,可以在其中发送多个DeviceId的有效负载以更新其设置.例如,我要发送的示例有效负载是:
I have an API in node.js, where I can send payloads for multiple DeviceIds, to update their settings. For instance, a sample payload that I would send is:
{"DeviceId":["1","2","3"],"Settings":[{"Key":"OnSwitch","Value":"true"}]}
发送后,我会说DeviceId 1、2、3都将更新其设置.这可以正常工作,我已经在Postman中进行了本地测试.我现在想编写一个单元测试来检查行为.我的单元测试如下:
After sending it, I would say that the DeviceId 1,2,3 all will have updated their settings. This is working correctly and I've tested it locally in Postman. I now want to write a unit test to check the behaviour. My unit test is the following:
context('POST With Multiple IDs', () => {
describe('1,2,3 IDs POST', () => {
it.only('It should post a full payload with Value True', (done) =>{
chai.request('http://localhost:3999')
.post('/api/v2/settings/')
.set('Authorization', authToken)
.set('Content-Type', 'application/json')
.type('Form')
.send({"DeviceId": ["1", "2", "3"],
"Settings": [
{
"Key": "OnSwitch",
"Value": "true"
}
]
})
.then(res => {
console.log(res.body);
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('message');
res.body.should.have.property('message').eql('All of the Ids Settings sent for processing.');
res.body.should.have.property('payload');
done();
});
});
});
在发布帖子的代码中,我检查DeviceIds是否出现在数组中.如果它们不在数组中,则会返回一条消息,指出DeviceId必须在数组中.这是该代码:
In the code, where I make the post, I check that the DeviceIds appear in an array. If they are not in an array a message is returned saying that the DeviceIds must be in an array. Here is the code for that:
if (!Array.isArray(req.body.DeviceId)) {
logInfo({message: 'DeviceId must be an array.', data: req.body}, 'API');
return res.status(400).send({
message: 'DeviceId must be an array.',
});
}
当我运行上面的单元测试时,我陷入了这个if语句中,并且得到了如下消息: req.body.DeviceId
不是数组.如果我记录 typeof req.body.DeviceId
,则会得到未定义的记录.为什么会这样呢?当我在邮递员本地发布帖子时,效果很好.那就是当我在本地发送帖子时,帖子请求顺利通过.但是现在使用 chaiHttp
进行单元测试,我有点麻烦.可能有人知道为什么在单元测试中会发生这种情况,并使其成为现实,这样就不会发生这种情况,而我也不会陷入此阵列检查中.任何建议将不胜感激!
When I run the unit test above, I get caught in this if statement and I get the message back that the req.body.DeviceId
is not an array. If I log the typeof req.body.DeviceId
, I get undefined logged. Why would that be? It's working fine when I do a post locally from postman. That is when I send a post locally, the post request goes through without error. But using chaiHttp
for the unit test now, I'm having a bit of trouble. Might anyone know why this is happening in the unit test and to make it so that it wouldn't be happening I wouldn't be getting caught in this Array Check. Any advice would be much appreciated!
推荐答案
单元测试中的 .type('Form')
行取消了将内容类型设置为application/json的操作.该行实际上将内容类型更改为无法识别数组的其他内容类型.因此,删除该行并进行以下单元测试是可行的!
The line .type('Form')
in the unit test cancels out setting the Content Type to application/json. That line actually changes the content type to a different content type which doesn't recognize arrays. Hence, removing that line and having the following unit test instead worked!
context('POST With Multiple IDs', () => {
describe('1,2,3 IDs POST', () => {
it.only('It should post a full payload with Value True', (done) =>{
chai.request('http://localhost:3999')
.post('/api/v2/settings/')
.set('Authorization', authToken)
.set('Content-Type', 'application/json')
.type('Form')
.send({"DeviceId": ["1", "2", "3"],
"Settings": [
{
"Key": "OnSwitch",
"Value": "true"
}
]
})
.then(res => {
console.log(res.body);
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('message');
res.body.should.have.property('message').eql('All of the Ids Settings sent for processing.');
res.body.should.have.property('payload');
done();
});
});
});
这篇关于如何在JSON对象中发送数组以进行ChaiHttp单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!