我试图运行Mocha测试,但我总是得到错误:
Unhandled rejection Error: pool is draining and cannot accept work
at Pool.acquire (D:\Codes\Node\posgmvc\node_modules\generic-
pool\lib\generic-pool.js:385:11)
at D:\Codes\Node\posgmvc\node_modules\knex\lib\client.js:281:19
at Promise._execute (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\debuggability.js:299:9)
at Promise._resolveFromExecutor (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\promise.js:481:18)
at new Promise (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\promise.js:77:14)
at Client_PG.acquireConnection (D:\Codes\Node\posgmvc\node_modules\knex\lib\client.js:272:12)
at D:\Codes\Node\posgmvc\node_modules\knex\lib\runner.js:199:23
at Promise._execute (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\debuggability.js:299:9)
at Promise._resolveFromExecutor (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\promise.js:481:18)
at new Promise (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\promise.js:77:14)
at D:\Codes\Node\posgmvc\node_modules\knex\lib\runner.js:198:35
at tryCatcher (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\util.js:16:23)
at Function.Promise.attempt.Promise.try (D:\Codes\Node\posgmvc\node_modules\bluebird\js\release\method.js:39:29)
at Runner.ensureConnection (D:\Codes\Node\posgmvc\node_modules\knex\lib\runner.js:197:34)
at Runner.run (D:\Codes\Node\posgmvc\node_modules\knex\lib\runner.js:47:42)
at Builder.Target.then (D:\Codes\Node\posgmvc\node_modules\knex\lib\interface.js:35:43)
at Context.<anonymous> (D:\Codes\Node\posgmvc\test\article.js:21:30)
at callFnAsync (C:\Users\Ashutosh\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:366:21)
at Hook.Runnable.run (C:\Users\Ashutosh\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:316:7)
at next (C:\Users\Ashutosh\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:309:10)
at Immediate._onImmediate (C:\Users\Ashutosh\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:339:5)
at tryOnImmediate (timers.js:543:15)
at processImmediate [as _immediateCallback] (timers.js:523:5)
这是我的测试:
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';
//Require the dev-dependencies
var chai = require('chai'),
chaiHttp = require('chai-http'),
server = require('../app'),
db = require('../app/db');
var should = chai.should();
chai.use(chaiHttp);
var id;
describe('Article URLs', function () {
//Before each test we empty the database
beforeEach(function (done) {
db("articles").del().then(function (count) {
console.log(count);
}).finally(function () {
db.destroy();
});
done();
});
describe('/POST article', function () {
it('it should create new article', function (done) {
chai.request(server)
.post('/api/articles')
.send({
title: 'test title',
url: 'some url',
text: 'some text'
})
.end(function (err, res) {
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property('id');
res.body.should.have.property('message');
res.body.message.should.equals('done');
id = res.id;
done();
});
});
});
describe('/PUT article', function () {
it('it should update article', function (done) {
chai.request(server)
.put('/api/articles/' + id)
.send({
title: 'new title',
url: 'new url',
text: 'new text'
})
.end(function (err, res) {
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property('id');
res.body.should.have.property('message');
res.body.message.should.equals('done');
done();
});
});
});
describe('/GET articles', function () {
it('it should return all articles', function (done) {
chai.request(server)
.get('/api/articles')
.end(function (err, res) {
res.should.have.status(200);
res.should.be.json;
res.body.should.be.a('array');
res.body.length.should.be.greaterThan(0);
res.body.should.have.property('articles');
res.body.should.have.property('message');
res.body[0].should.have.property('id');
res.body[0].should.have.property('title');
res.body[0].should.have.property('text');
done();
});
});
});
describe('/GET article', function () {
it('it should return single article', function (done) {
chai.request(server)
.get('/api/articles/' + id)
.end(function (err, res) {
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property('article');
res.body.should.have.property('message');
res.body.should.have.property('id');
res.body.should.have.property('title');
res.body.should.have.property('text');
done();
});
});
});
describe('/DELETE article', function () {
it('it should delete single article', function (done) {
chai.request(server)
.delete('/api/articles/' + id)
.end(function (err, res) {
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property('count');
res.body.should.have.property('message');
res.body.count.should.be.greaterThan(0);
res.body.message.should.be.equals('found');
done();
});
});
});
});
这是我的knexfile.js:
module.exports = {
development: {
client: 'postgresql',
connection: {
database: 'posgmvc-development',
user: 'postgres',
password: 'pa$$w0rd'
},
pool: {
min: 2,
max: 10
}
},
test: {
client: 'postgresql',
connection: {
database: 'posgmvc-test',
user: 'postgres',
password: 'pa$$w0rd'
},
pool: {
min: 2,
max: 10
}
}
};
答案 0 :(得分:1)
我对Knex没有任何经验,但这看起来并不正确:
beforeEach(function (done) {
db("articles").del().then(function (count) {
console.log(count);
}).finally(function () {
db.destroy();
});
done();
});
删除所有文章后,您将销毁数据库,并且此代码在每次测试之前运行。因此,对于第二个测试,当运行此代码时,db
已被销毁/无效。
试试这个(同样,它使用Mocha的内置承诺支持代替使用done
):
beforeEach(function() {
return db("articles").del().then(function (count) {
console.log(count);
});
});