簡體   English   中英

如何使用Chai檢查從API提取的數組中元素的屬性是否等於某個值?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM