我正在创建 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']);
它们都不起作用
最佳答案
有点晚了,但对于任何 future 的观众,您需要这样的东西:
res.body.should.have.property('data').that.includes.all.keys(['status', 'id', 'name', 'email',
'bio', 'image', 'email_verified', 'role', 'isActive', 'isDeleted', 'createdAt', 'updatedAt', 'token']);
关于mocha.js - Mocha Chai 检查 Json 响应是否包含特定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57275174/