Search code examples
mocha.jsbddrethinkdbchai

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


I'm using the chai expect library for my tests. I have an array of objects which is the test data. Each object has 2 properties name and profession. I inject these into a table. When I retrieve all records from the get the same array back, but now every object in the array has been added with an auto generated id field. I need to verify my test data against the retrieved data. Is there any shorthand way of doing this in chai without having to iterate through the retrieved data?


Solution

  • You can use without to eliminate a field in result:

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

    THat way you can assert against it easily.

    Example code:

    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)
      })