我有一个验证函数,使用expect
断言检查不同的表单字段(检查字段是否为空)。如果捕获到错误,将显示一条错误消息,指出哪个字段的断言失败。这工作正常。
但是我无法检查数组中元素的属性是否等于某个值。这就是我想要做的事情:
function validateNameAndToken (campaign) {
let campaignName = campaign.name;
try {
expect(campaign.name, 'Campaign name can not be empty').to.not.be.empty
// More expect assertions that are more or less similar
// This is the assertion that should somehow check if the array contains
// an element with its name property equal to campaignName
expect(API.campaigns, 'The campaign name can not be repeated').to.not.be.an('array').that.includes(campaign);
}
catch (err) {
console.log('Campaign#validateNameAndToken - catch: ', err)
return { isValid: false, msg: err.message }
}
return { isValid: true }
}
我见过Chai的文档,我看到如何检查一个数组是否包含一个元素,并查看一个对象是否包含一个属性......但是还是无法将所有部分放在一起...