[英]How to send array in JSON object for ChaiHttp Unit Testing?
I have an API in node.js, where I can send payloads for multiple DeviceIds, to update their settings.我在 node.js 中有一个 API,我可以在其中发送多个 DeviceId 的有效负载,以更新它们的设置。 For instance, a sample payload that I would send is:例如,我要发送的示例有效负载是:
{"DeviceId":["1","2","3"],"Settings":[{"Key":"OnSwitch","Value":"true"}]}
After sending it, I would say that the DeviceId 1,2,3 all will have updated their settings.发送后,我会说 DeviceId 1,2,3 都将更新他们的设置。 This is working correctly and I've tested it locally in Postman.这工作正常,我已经在 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();
});
});
});
In the code, where I make the post, I check that the DeviceIds appear in an array.在我发帖的代码中,我检查了 DeviceIds 是否出现在数组中。 If they are not in an array a message is returned saying that the DeviceIds must be in an array.如果它们不在数组中,则会返回一条消息,说明 DeviceId 必须在数组中。 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.',
});
}
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 语句,我得到了req.body.DeviceId
不是数组的消息。 If I log the typeof req.body.DeviceId
, I get undefined logged.如果我记录typeof req.body.DeviceId
,我会得到未定义的记录。 Why would that be?为什么会这样? It's working fine when I do a post locally from postman.当我从 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.但是现在使用chaiHttp
进行单元测试,我遇到了一些麻烦。 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!任何建议将不胜感激!
The line .type('Form')
in the unit test cancels out setting the Content Type to application/json.单元测试中的.type('Form')
行取消了将 Content Type 设置为 application/json。 That line actually changes the content type to a different content type which doesn't recognize arrays.该行实际上将内容类型更改为不识别 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();
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.