2

I'm creating API test which checks that the response contains specific key "textId" in structure:

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

response is following:

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

I tried:

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 of them works

destr
  • 25
  • 5
  • The body *doesn't* e.g. have that property, it *contains an object* that has that property. The body is an array, try e.g. `res.body[0].should...` or use array matchers. – jonrsharpe Jul 30 '19 at 15:30
  • `res.body[0].should...` returns Cannot read property 'should' of undefined, but now I found that this works correctly `res.body.should.have.nested.property('[0].textId');` thanks jonrsharpe – destr Jul 30 '19 at 16:10

1 Answers1

3

It's a bit late but for any future viewers, you need something like this:

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