2016-11-18 68 views
0

我爲我的API創建集成測試,遇到了以下錯誤:快速API集成測試:錯誤:超過2000ms的超時。確保完成()回調被稱爲在本次測試

Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test

我知道這個問題已經被問了幾次,但答案並沒有幫助我解決這個問題。有問題的測試是測試一個POST路線,並進行回調被稱爲:

it('should create a transaction', function(done) { 
    request(app) 
     .post('/api/transactions') 
     .send({ 
     name: 'Cup of coffee', 
     amount: 2.50, 
     date: '2016-11-17T17:08:45.767Z' 
     }) 
     .set('Accept', 'application/json') 
     .expect('Content-Type', /json/) 
     .expect(201) 
     .end(function(err, resp) { 
     expect(resp.body).to.be.an('object'); 
     done(); 
     }) 
    }) 

員額路線如下:

.post(function (req, res) { 
    var transaction = new Transaction() 
    transaction.name = req.body.name 
    transaction.amount = req.body.amount 
    transaction.date = req.body.date 

    transaction.save(function (err) { 
     if (err) { 
     res.send(err) 
     } 
     res.json(transaction) 
    }) 
    }) 

貓鼬模式的交易:

var mongoose = require('mongoose') 
var Schema = mongoose.Schema 

var TransactionsSchema = new Schema({ 
    name: String, 
    amount: Number, 
    date: { type: Date, default: Date.now } 
}, { 
    collection: 'transactions' 
}) 

module.exports = mongoose.model('Transactions', TransactionsSchema) 

任何想法?謝謝:)

回答

1

內部測試,你可以指定測試timeout

it('should create a transaction', function(done) { 
    // Specify a timeout for this test 
    this.timeout(30000); 

    request(app) 
     .post('/api/transactions') 
     .send({ 
     name: 'Cup of coffee', 
     amount: 2.50, 
     date: '2016-11-17T17:08:45.767Z' 
     }) 
     .set('Accept', 'application/json') 
     .expect('Content-Type', /json/) 
     .expect(201) 
     .end(function(err, resp) { 
     expect(resp.body).to.be.an('object'); 
     done(); 
     }) 
    }); 
+0

男人啊,非常酷 - 謝謝你。所以只是我的筆記本電腦太舊了,運行測試需要很長時間? :) – zeKoko