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

标签 node.js express mocha.js

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

相关文章:

javascript - 如何在 Mocha 测试用例中发送 header ('Authorization' ,'Bearer token' )

node.js - Node.js 的 PayPal 模块

node.js - 检查 serverless.yml 文件中的环境变量(无服务器框架)

node.js - 如何杀死重新启动的进程? (永远)

javascript - 当我尝试将中间件函数导出到 module.exports 对象时,为什么会收到 'Next is not a function'?

javascript - Google Cloud 上的虚拟 Ubuntu VM 无法通过 nodejs、外部 ip 连接

javascript - 通过 POST 接收 JSON 的 API 接收到的类型与发送的类型不同

node.js - 发布请求 Firebase 云功能超时

javascript - Node 模块的映射路径,用于单元测试

javascript - 使用 Mocha & Chai 进行 Node 测试 -> 错误.. TypeError : app. 地址不是函数