Express API集成测试:错误:超出2000ms的超时。确保在此测试中调用done()回调

时间:2016-11-18 22:30:41

标签: node.js express testing chai supertest

我正在为我的api创建集成测试,并遇到以下错误:

  

错误:超过2000毫秒的超时。确保正在进行done()回调   在这个测试中调用

我知道这个问题已被问过几次,但答案并没有帮助我解决这个问题。有问题的测试是测试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)
    })
  })

交易的Mongoose Schema是:

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 个答案:

答案 0 :(得分: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();
      })
  });