Mocha Chai - 测试对象的多个属性

时间:2018-03-14 14:33:27

标签: mocha chai

我想检查我的响应对象是否包含使用chai should assertion提到的属性。

以下是我的代码段:

chai.request(app)
        .post('/api/signup')
        .send(
            data
        )
        .then(function (response) {
            response.should.have.status(200);
            response.body.should.have.property('active', 'mobileNumber', 'countryCode', 'username', 'email', 'id', 'createdAt', 'updatedAt', 'location');
            done();
        })
        .catch(function (error) {
            done(error);
        })

但我收到以下错误: enter image description here

2 个答案:

答案 0 :(得分:4)

使用mocha chai-should,

找到了如何实现它

功能名称为.should.have.keys() 只需将属性传递给此函数,它就会检查它们是否应出现在您正在测试的对象中。

贝娄是代码

response.body.should.have.keys('active', 'mobileNumber', 'countryCode', 'username', 'email', 'id', 'organisationId', 'createdAt', 'updatedAt', 'location');

答案 1 :(得分:0)

如果您专门使用Chai(例如在Postman中),则以下函数字符串将为您提供成功的帮助:

response.body.to.include.all.keys("active", "mobileNumber");

The full list of API's (including an example of the above) can be found here.

相关问题