gpt4 book ai didi

node.js - Express API 集成测试 : Error: timeout of 2000ms exceeded. 确保在此测试中调用 done() 回调

转载 作者:行者123 更新时间:2023-11-28 21:25:56 25 4
gpt4 key购买 nike

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

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

我知道这个问题已被问过几次,但答案并没有帮助我解决这个问题。有问题的测试是测试 POST 路由,并且正在调用 done 回调:

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)

有什么想法吗?谢谢:)

最佳答案

在您的测试中,您可以指定测试 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();
})
});

关于node.js - Express API 集成测试 : Error: timeout of 2000ms exceeded. 确保在此测试中调用 done() 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40686938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com