2016-10-17 83 views
0

我試圖運行摩卡測試,但我總是得到錯誤:摩卡測試水渠的Postgres連接池

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 
    } 
} 
}; 

回答

1

我沒有與任何Knex經驗,但是這看起來不正確:

beforeEach(function (done) { 

    db("articles").del().then(function (count) { 
     console.log(count); 
    }).finally(function() { 
     db.destroy(); 
    }); 

    done(); 
}); 

一旦所有文章都被刪除,並且此代碼在每次測試之前運行,就會銷燬數據庫。因此,對於第二次測試,運行此代碼時,db已被銷燬/失效。

試試這個(也,而不是使用done,它採用摩卡內置的承諾支持):

beforeEach(function() { 
    return db("articles").del().then(function (count) { 
    console.log(count); 
    }); 
}); 
+0

非常感謝羅伯特,這是一個愚蠢的錯誤:) – Ashutosh