【问题标题】:mocha 'after' fails saying it can't find 'app'摩卡'之后'失败说它找不到'应用'
【发布时间】:2016-06-11 05:53:26
【问题描述】:

好的,如果我注释掉 'before' 和 'after' 方法,我的 mocha 测试将通过。我确信我的两个错误是相互关联的。

'after' 方法失败,说明 app.close 不是函数。 “之前”方法失败,说它在我的第 7 行找不到“应用程序”(清除服务器缓存)。

我完全没有选择或想法。我希望能够按照我的命令启动和停止我的服务器。这是我第一次尝试在我的 mocha 测试中包含任何类型的“之前/之后”方法。下面的工作代码,但我失败的部分被注释掉了。有什么建议吗??

var request = require('supertest');
var app = require('../../server');

describe('server', function() {
    before(function () {
        //var app = require('../../server')();
        //delete require.cache[require.resolve('app')];
    });
    after(function () {
        //app.close();
    });
    describe('basic comms', function() {
        it('responds to root route', function testSlash(done) {
            request(app)
                .get('/')
                .expect('Content-type', /json/)
                //.expect(res.message).to.equal('Hello World!')
                .expect(200, done);
        });
        it('404 everything else', function testPath(done) {
            //console.log('testing 404 response');
            request(app)
                .get('/foo/bar')
                .expect(404, done);
        });
    });
});

【问题讨论】:

    标签: express mocha.js


    【解决方案1】:

    before 中,您需要应用程序的方式与第 2 行不同。为什么不使用已经需要的应用程序?

    例子:

    before(function () {
        // here you can use app from line 2
    });
    

    关于app.close,你在哪里找到这个功能的?

    查看 Express 文档: http://expressjs.com/en/4x/api.html#app

    要关闭快递服务器,可以使用这种方法:

    how to properly close node-express server?

    【讨论】:

    • 谢谢@Andrei。我通过最后一个链接接受了您的建议并使用了它。至于将第 2 行留在那里来引用我的服务器,我这样做是为了在需要时激活它或将其注释掉,以测试我的“它”是否仍然有效,而我的“之前”或“没有任何东西”后'。既然您的建议被证明很棒,我已经将该行永久移至我的“之前”。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    相关资源
    最近更新 更多