Node.js 摩卡咖啡,快速测试错误-“;毕竟";钩子错误-对象函数没有方法';关闭';
我刚刚开始使用Mocha,测试一个非常基本的Express4.0RESTAPINode.js 摩卡咖啡,快速测试错误-“;毕竟";钩子错误-对象函数没有方法';关闭';,node.js,express,mocha.js,Node.js,Express,Mocha.js,我刚刚开始使用Mocha,测试一个非常基本的Express4.0RESTAPI describe('API CALL UNIT TESTING', function(){ var app = require('../../app'); before(function(){ app.listen(3000); }); describe('GET', function(){ it('respond with json', funct
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次通过(48ms)1次失败
1) API调用单元测试“毕竟”挂钩:
类型错误:对象函数(req、res、next){
app.handle(req,res,next);}没有方法“close”
有人能告诉我是什么导致了“毕竟”钩子错误吗?显然,app对象没有close()方法。实际上,您并没有确切地告诉我们什么是应用程序,但如果我正确地回忆起express API,您实际上调用了从listen()返回的对象上的close(),因此您可以尝试:
var server;
before(function(){
server = app.listen(3000);
});
....
after(function() {
server.close();
});
非常感谢。的确,您是正确的,对从listen()返回的对象调用close。