Javascript 把它锁在柴
我有一个js文件,它提供了一些db操作。此文件仅适用于可链接的承诺。为了测试这个类,我使用了一个异步函数 问题是,每当我在测试函数中使用承诺时,Javascript 把它锁在柴,javascript,node.js,mocha.js,chai,Javascript,Node.js,Mocha.js,Chai,我有一个js文件,它提供了一些db操作。此文件仅适用于可链接的承诺。为了测试这个类,我使用了一个异步函数 问题是,每当我在测试函数中使用承诺时,it函数在以后的每一次测试中都会被阻塞 以下是两个例子: 'use strict' const exec = require('child_process').exec const path = require('path') const request = require('request') const expect = require('chai
it
函数在以后的每一次测试中都会被阻塞
以下是两个例子:
'use strict'
const exec = require('child_process').exec
const path = require('path')
const request = require('request')
const expect = require('chai').expect
const createTableStatements = require('../data')
test()
async function test () {
await testGetUser()
console.log('1')
await testGetFaculties()
}
function testGetUser () {
return new Promise((resolve1) => {
describe('test get user', function () {
const db = require('../dbInterface')
it('test get user should be complete', function () {
db.dbFunctions.dropAll()
.then(onResolve => {
return db.dbFunctions.createTable(createTableStatements.createTableStatements.user)
}
)
.then(() => {
console.log('success create user table')
return db.dbFunctions.addUser('1', 'firstName', 'lastName', 'email')
})
.then(resolve => {
return db.dbFunctions.getUser('email', undefined)
})
.then(result => {
expect(result.toString().includes('dummy')).to.equal(false)
})
.then(resolve => {
return db.dbFunctions.dropAll()
})
.then(resolve => {
console.log('resolve')
resolve1()
})
.catch(err => console.log(err))
})
})
})
}
function testGetFaculties () {
return new Promise(resolve => {
describe('test get faculties', function () {
let db
before(function () {
db = require('../dbInterface')
})
console.log('displayed')
it('should work', function () {
console.log('locked')
expect(db.dbFunctions.getFaculties('hsa')).to.be.an('array').that.does.include('Science')
resolve()
})
})
})
}
这就是输出
resolve
1
displayed
如您所见,console.log('locked')未被处理。
到目前为止,我发现只有在调用then函数中的expect
时才会出现这个问题。但这对我的测试是必要的
test()
函数应该包含更多的测试,只是为了这个问题我缩短了它
澄清一下:如果我只测试类型为testGetFaculties()
的方法,它内部不包含另一个承诺链,它的工作方式应该是这样的
知道为什么会这样吗?很可能是
console.log('locked')代码>没有做任何事情,因为您以前的测试用例根本没有完成
在承诺内写入描述
,它
,之前,包含未回复的承诺是不应该做的事情
更好的测试用例如下所示:
'use strict'
const exec = require('child_process').exec
const path = require('path')
const request = require('request')
const expect = require('chai').expect
const createTableStatements = require('../data')
// You use this in both test cases anyway
const db = require('../dbInterface');
describe('test get user', function () {
it('test get user should be complete', function () {
return db
// ^ returning promise will make sure that the test ends when the promise ends.
.dbFunctions
.dropAll()
.then(onResolve => { ... } )
...
)
} );
} );
describe('test get faculties', function () {
it('should work', function () {
return db
// ^ returning promise will make sure that the test ends when the promise ends.
.dbFunctions
.getFaculties('hsa')
.then( value => {
// ^ You actually need to test the value of the resolve promise
expect( value ).to.be.an('array').that.does.include('Science');
} )
} );
} );
不要使用newpromise()
。你不知道我是多么努力地用descripe only而不是nested promissed来工作,而回报承诺一直是我错过的东西!