如何在 JSON 对象中发送数组以进行 ChaiHttp 单元测试? [英] How to send array in JSON object for ChaiHttp Unit Testing?

查看:29
本文介绍了如何在 JSON 对象中发送数组以进行 ChaiHttp 单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在我发布帖子的代码中,我检查 DeviceId 是否出现在数组中.如果它们不在数组中,则会返回一条消息,指出 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') 行取消了将 Content Type 设置为 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆