【问题标题】:How to check if array contains defined values using supertest?如何使用超测试检查数组是否包含定义的值?
【发布时间】:2021-12-25 23:02:06
【问题描述】:

假设在测试期间我有这样的数组:

array = [
 'a',   'b',
 'c',   'd',
 'e',   'f',
 'g',   'h',
 'i',   'j'
]

我想检查这个数组是否真的包含这些值:

array.should.be.a.Array()
.with.lengthOf(response.body.length)
.and.have.properties('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');

如您所见,我不能在这里使用have.properties,因为它是数组,而不是对象。那么,我该如何检查呢?

【问题讨论】:

    标签: javascript node.js supertest should.js


    【解决方案1】:

    如果您需要精确匹配,请使用 Chandan 的答案,如果您需要匹配子集,可以使用 should.containDeep(请参阅 docs)。这是一个例子:

    const arr = [
     'a',   'b',
     'c',   'd',
     'e',   'f',
     'g',   'h',
     'i',   'j'
    ];
    
    arr.should.containDeep(['a', 'b', 'c', 'd']);
    console.log("test passed");
    <script src="https://cdnjs.cloudflare.com/ajax/libs/should.js/13.2.3/should.min.js"></script>

    【讨论】:

      【解决方案2】:

      您可以使用should.deepEqual 将长度和值与顺序匹配

      const arr = [
       'a',   'b',
       'c',   'd',
       'e',   'f',
       'g',   'h',
       'i',   'j'
      ];
      
      arr.should.deepEqual(arr);
      console.log("test passed");
      <script src="https://cdnjs.cloudflare.com/ajax/libs/should.js/13.2.3/should.min.js"></script>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-24
        • 2018-04-25
        • 2016-02-04
        • 1970-01-01
        • 1970-01-01
        • 2015-10-16
        • 1970-01-01
        相关资源
        最近更新 更多