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

Mocha Chai check if Json response contains specific property

我正在创建 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']);

none 作品

有点晚了,但对于任何未来的观众,您需要这样的东西:

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