[英]How to check if the property of an element in an array fetched from the API is equal to certain value using Chai?
我有一个使用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的文档,看到如何检查数组是否包含元素,以及如何查看对象是否包含属性...但是还不能将所有部分放在一起...
您可能需要使用.deep()。 查看文档 。 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.