node.js - Mocha - 哪个先运行,beforeEach 还是 before?

标签 node.js express mocha.js

我有一个具有如下规范的网络应用程序:

describe('Hook them up', () => {

    var server;

    beforeEach(done => {
        server = app.listen(done);
    });

    before(done => {
       // Does this run before or after "beforeEach"
       // If I try to access the api at this point I get an ECONNREFUSED
    });

    after(done => {
        server.close(done);
    });


    it('should set the \'createdAt\' property for \'DndUsers\' objects', done => {
        api.post('/api/tweets')
            .send({ text: 'Hello World' })
            .then(done)
            .catch(err => {
                console.log(err);
                done();
            });
    });
});

在我的其他一些项目中,如果我尝试访问 before block 中的 api,它工作正常,就好像 beforeEach 已经运行一样。

最佳答案

See my answer here一个非常相似的问题。

Mocha 的测试运行器在 Hooks section of the Mocha Test Runner 中对这个功能进行了最好的解释。 .

来自 Hooks 部分:

describe('hooks', function() {

    before(function() {
        // runs before all tests in this block
    });

    after(function() {
        // runs after all tests in this block
    });

    beforeEach(function() {
        // runs before each test in this block
    });

    afterEach(function() {
        // runs after each test in this block
    });

    // test cases
    it(...);  // Test 1
    it(...);  // Test 2
});

您可以将这些例程嵌套在其他描述 block 中,这些 block 也可以有 before/beforeEach 例程。

这应该给你

hooks
    before
        beforeEach
            Test 1
        afterEach
        beforeEach
            Test 2
        afterEach
    after

关于node.js - Mocha - 哪个先运行,beforeEach 还是 before?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36755836/

相关文章:

android - Cordova问题无法添加平台android

javascript - Node 应用程序架构 : where does express come into node. js

node.js - 快速路由器 - :id?

javascript - Angular .min.js :63 ReferenceError

node.js - 循环 Mocha 测试

android - Mongoose 和 Android Volley

node.js - 如何删除DynamoDB中的多行?

unit-testing - 使用 jQuery-Chai 测试元素

node.js - 使用 frisby.js 生成 Rest API 测试的测试覆盖率

javascript - 在node-express应用程序中使用node-mysql建立共享连接