How to send array in JSON object for ChaiHttp Unit Testing?

不问归期 提交于 2020-12-15 05:20:33

问题


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"}]}

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();
        });
    });
});

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.',
                });
            }

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!


回答1:


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();
        });
    });
});


来源:https://stackoverflow.com/questions/65239249/how-to-send-array-in-json-object-for-chaihttp-unit-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!