如何验证对象数组中的每个对象是否都有一些键

How to verify if every object in an array of objects has some keys

我正在使用 chai expect 库进行测试。我有一个对象数组,它是测试数据。每个对象都有 2 个属性 nameprofession。我将这些注入 table。当我从相同的数组中检索所有记录时,但现在数组中的每个对象都添加了一个自动生成的 id 字段。我需要根据检索到的数据验证我的测试数据。有没有 shorthand 在 chai 中执行此操作而不必遍历检索到的数据的方法?

您可以使用without来消除结果中的一个字段:

r.table('test').without('id')

这样你就可以很容易地反对它。

示例代码:

var chai   = require('chai')
var assert = chai.assert

var r      = require('rethinkdb')

r.connect({
   host: 'localhost',
       port: 28015,
  })
  .then(function(conn) {
    return conn
  })
  .then(function(conn) {
    return r.table('table').without('id').run(conn)
  })
  .then(function(cursor) {
    return cursor.toArray()
  })
  .then(function(data) {
    assert.deepEqual([
     {name: 'foo', profession: 'bar'},
     {name: 'foo2', profession: 'bar2'},
    ], data)
  })