我刚刚开始使用 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/