2015-12-16 39 views
0

试图围绕这一点我的头,它把我的思想在异步循环。异步与RethinkDB和柴

运行这个简单的测试后,手动检查RethinkDB的结果是否正确(db'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']) 
      }) 
      }) 
     }) 
     }) 
    }) 
    }) 
}) 
+0

我相信你应该使用'expect(result).to.deep.equal(...)'。直接比较Javascript中的数组并不那么简单。 – Tryneus

+2

至于异步问题,你的测试用例没有采用'done'回调,所以框架假设你的测试是同步的。为了解决这个问题,测试用例声明应该是'it('建立连接',(done)=> ...)',并且你应该在'expect'后面调用'done()'。 – Tryneus

+0

@Tryneus谢谢。查看修改后的代码。还有一些问题。 – RichardForrester

回答

0

我认为这可以工作,但最内部的回调不会被执行,测试只是超时。

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