Chai - 应该有 属性 - 超测

Chai - Should have Property - Supertest

正在尝试检查 属性 是否存在并且它不是空字符串。

我可以让这个测试正常工作:

it('the data includes a list of items', function(done){
    body.should.have.property('items').and.to.be.an('array');
    done();
});

但是下面,当我尝试检查项目数组中存在的属性时,我无法让它工作:

it('each item should include properties \'label\' and \'url\'', function(done){
    body['items'].should.have.property('label').and.to.be.a('string');
    body['items'].should.have.property('url').and.to.be.a('string');
    done();
});

但我收到错误 AssertionError: expected [ Array(3) ] to have a property 'label'

返回的 JSON 对象如下所示:

{
  "items": [
    {
      "label": "Item 1",
      "url": "http://www.something.com"
    },
    {
      "label": "Item 2",
      "url": "http://www.something.com"
    },
    {
      "label": "Item 3",
      "url": "http://www.something.com"
    }
  ]
}

试试这个:

    it('each item should include properties \'label\' and \'url\'', function(done){ 
        var firstItem = body.items[0];
        firstItem.should.have.property('label').and.to.be.a('string');
        firstItem.should.have.property('url').and.to.be.a('string');
        done();
            });