首先我的英语不好,但这个测试对我来说失败了。
test('get user', async t => {
let db = t.context.db
t.is(typeof db.getUser, 'function', 'getUser is a function')
let user = fixtures.getUser()
let created = await db.saveUser(user)
let result = await db.getUser(user.username)
t.deepEqual(created, result)
// t.throws(db.getUser('foo'), /not found/)
})
并返回这个
1 failed
db-test » get user
Test finished, but an assertion is still pending
如果我删除该行
t.throws(db.getUser('foo'), /not found/)
测试运行没有任何问题。
测试的功能是这个
getUser (username, callback) {
if (!this.connected) {
return Promise.reject(new Error('not connected')).asCallback(callback)
}
let connection = this.connection
let db = this.db
let tasks = co.wrap(function * () {
let conn = yield connection
yield r.db(db).table('users').indexWait().run(conn)
let users = yield r.db(db).table('users').getAll(username, {
index: 'username'
}).run(conn)
let result = null
try {
result = yield users.next()
} catch (e) {
return Promise.reject(new Error(`user ${username} not found`))
}
return Promise.resolve(result)
})
return Promise.resolve(tasks()).asCallback(callback)
}
该函数的作用是,如果它没有在数据库中获取用户,那么它会返回一个拒绝承诺并且它实际上会发生但之后我不知道会发生什么