【发布时间】:2016-03-21 23:47:12
【问题描述】:
试图把我的头脑围绕在这个问题上,这让我陷入了一个异步循环。
运行这个简单的测试后,我手动检查 RethinkDB 的结果是正确的(在 db 'test' 中有一个名为 'books' 的表);但是,无论我在 chai 的 expect 函数中断言什么,这个测试都会通过。我知道这是一个异步问题,因为 console.log(result) 在测试完成后会打印到控制台。我原以为 expect 会在 Rethink 得到 tableList() 后运行,因为它在回调中。
- 为什么这个测试通过(它应该在
['books'] === ['anything']失败? - 为什么
expect()不在tableList()之后运行? - 将命令链接到 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'])
})
})
})
})
})
})
})
【问题讨论】:
-
我相信你应该使用
expect(result).to.deep.equal(...)。在Javascript中直接比较数组并不是那么简单。 -
至于异步问题,您的测试用例没有采用
done回调,因此框架假定您的测试是同步的。为了解决这个问题,测试用例声明应该是it('makes a connection', (done) => ...),你应该在expect之后调用done()。 -
@Tryneus 谢谢。查看修改后的代码。还有一些问题。
-
@Tryneus 实际上,没关系,由于某种原因,它仍然没有读取最内部的回调。
-
帮助我理解的非常有用的链接:staxmanade.com/2015/11/…
标签: javascript node.js asynchronous rethinkdb chai