gpt4 book ai didi

node.js - Mocha,Express 测试错误 - "after all" Hook 错误 - 对象函数没有方法 'close'

转载 作者:太空宇宙 更新时间:2023-11-03 22:47:30 25 4
gpt4 key购买 nike

我刚刚开始使用 Mocha,正在针对非常基本的 Express 4.0 Rest API 进行测试。

describe('API CALL UNIT TESTING', function(){
var app = require('../../app');
before(function(){
app.listen(3000);
});

describe('GET', function(){

it('respond with json', function(done){
request(app)
.get('/api/compile')
.set('Accept', 'application/json')
.expect('Content-Type', 'application/json')
.expect(200, done)
.end(function(e, res){
//console.log(res)
done();
})
})
});

after(function() {
app.close();
});
});

运行测试时出现以下错误:

1 passing (48ms) 1 failing

1) API CALL UNIT TESTING "after all" hook: TypeError: Object function (req, res, next) { app.handle(req, res, next); } has no method 'close'

任何人都可以告诉是什么导致了“毕竟” Hook 错误吗?

最佳答案

显然,app 对象没有 close() 方法。您实际上并没有准确地告诉我们应用程序是什么 - 但如果我正确地记得express API,您实际上对从listen()返回的对象调用了close(),所以也许您可以尝试:

var server;

before(function(){
server = app.listen(3000);
});

....

after(function() {
server.close();
});

关于node.js - Mocha,Express 测试错误 - "after all" Hook 错误 - 对象函数没有方法 'close',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23880277/

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