Mocha Chai检查Json响应是否包含特定属性

时间:2019-07-30 15:26:36

标签: mocha chai jsonpath chai-http

我正在创建API测试,该测试将检查响应是否在结构中包含特定的键“ textId”:

  it('TC-4 /rest/passwords/ should change "password"', function() {
    return chai.request(serverurl)
      .post('/rest/passwords/')
      .set('Content-Type', 'application/json')
      .set('Accept', 'text/html')
      .set('X-Api-Key', global.apikey)
      .set('Cookie', global.cookie)
      .send({password: "password"})
      .then(function(res) {
        res.should.have.status(200);
        res.should.be.json;
        console.log('TC-4 /rest/passwords/: %j\n', res.body);
        res.body.should.have.all.keys(['textId']);
      });
  });

响应如下:

[{"textId":"PasswordNeedsAtLeastOneDigit","parameters":{}},{"textId":"PasswordNeedsAtLeastOneUpperCaseCharacter","parameters":{}}]

我尝试过:

res.body.should.have.property('textId');
res.body.should.have.nested.property('textId');
res.body.should.have.all.keys(['textId']);
res.body.should.have.all.nested.keys(['textId']);

它们都不起作用

1 个答案:

答案 0 :(得分:0)

有点晚了,但是对于以后的观众来说,您需要这样的东西:

res.body.should.have.property('data').that.includes.all.keys(['status', 'id', 'name', 'email',
          'bio', 'image', 'email_verified', 'role', 'isActive', 'isDeleted', 'createdAt', 'updatedAt', 'token']);