【问题标题】:Is there a way to make assertions only on selected array elements?有没有办法只对选定的数组元素进行断言?
【发布时间】:2021-06-08 07:31:59
【问题描述】:

假设我想检查一个数组是否包含一个元素,该元素具有满足给定断言的字段的特定值,例如:

{ array: [ { element1: Mario, element2: White }, { element1: Luigi, element2: Green } ] } 

这里我想检查元素 1 等于马里奥的数组元素的元素 2 等于白色。如何使用 chai/supertest(或其他 npm 包)做到这一点?

【问题讨论】:

    标签: mocha.js chai supertest


    【解决方案1】:

    你可以用 jsonpath 模块做到这一点:

    var jp = require('jsonpath');
    expect(jp.query(json, '$..array[?(@.element1=="Mario")]')[0].element2).to.equal('White')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-26
      • 2017-02-08
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 2011-06-24
      相关资源
      最近更新 更多