2016-04-20 56 views
2

我有這樣一個規範的web應用程序:Mocha - 哪個或哪個之前運行?

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(); 
      }); 
    }); 
}); 

在我的一些其他項目,如果我嘗試訪問API中的before塊它工作正常,好像beforeEach已經運行。

回答

4

See my answer here到一個非常類似的問題。

摩卡的測試運行者在Hooks section of the Mocha Test Runner中解釋了此功能中的最佳功能。

從鉤上一節:

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 
}); 

您可以巢內的其他這些程序描述塊也可以有前/ beforeEach例程。

這應該給你

hooks 
    before 
     beforeEach 
      Test 1 
     afterEach 
     beforeEach 
      Test 2 
     afterEach 
    after