与RethinkDB和Chai异步

Async with RethinkDB and Chai

本文关键字:异步 Chai RethinkDB      更新时间:2023-09-26

试图将我的头脑集中在这件事上,这让我陷入了异步循环。

在运行了这个简单的测试之后,当我手动检查RethinkDB时,结果是正确的(数据库"test"中有一个名为"books"的表);然而,无论我在chai的expect函数中断言什么,这个测试都通过了。我知道这是一个异步问题,因为console.log(result)在测试完成后打印到控制台。我本以为expect会在Rethink得到tableList()之后运行,因为它在回调中。

  1. 为什么此测试通过(它应该在['books'] === ['anything']处失败
  2. 为什么expect()不能在tableList()之后运行
  3. 将命令链接到RethinkDB以便按顺序执行的正确方法是什么

db_spec.js:

import {expect} from 'chai'
import r from 'rethinkdb'
describe('rethinkdb', () => {
  it('makes a connection', () => {
    var connection = null;
    r.connect({host: 'localhost', port: 28015}, function(err, conn) {
      if (err) throw err
      connection = conn
      r.dbDrop('test').run(connection, () => {
        r.dbCreate('test').run(connection, () => {
          r.db('test').tableCreate('books').run(connection, () => {
            r.db('test').tableList().run(connection, (err, result) => {
              if (err) throw err
              console.log(result)
              expect(result).to.equal(['anything'])
            })
          })
        })
      })
    })
  })
})

我认为这会起作用,但最内部的回调没有执行,测试只是超时了。

import {expect} from 'chai'
import r from 'rethinkdb'
describe('rethinkdb', () => {
  it('makes a connection', (done) => {
    var connection = null;
    r.connect({host: 'localhost', port: 28015}, function(err, conn) {
      if (err) throw err
      connection = conn
      r.dbDrop('test').run(connection, () => {
        r.dbCreate('test').run(connection, () => {
          r.db('test').tableCreate('books').run(connection, () => {
            r.db('test').tableList().run(connection, (err, result) => {
              if (err) throw err
              expect(result[0]).to.equal('books').done()
            })
          })
        })
      })
    })
  })
})