2016-04-21 63 views
3
describe('1', function() { 
    beforeEach(function() { 
    // do this before each it EXCEPT 1.5 
    }); 
    it('1.1', function() { 

    }); 
    it('1.2', function() { 

    }); 
    it('1.3', function() { 

    }); 
    it('1.4', function() { 

    }); 
    it('1.5', function() { 
    // beforeEach shouldn't run before this 
    }); 
}); 

我想阻止beforeEachit1.5之前运行。我怎样才能做到这一点?如何避免beforeEach在一个特定块之前运行?

+0

您可能可以在此线程获得更多见解http://stackoverflow.com/questions/32723167/how-to-programmatically-skip-a-test-in-mocha除了由chriskelly提供的答案 – samiunn

回答

2

选项1

我会建议使用嵌套的描述,例如:

describe('1', function() { 

    describe('1 to 4', function() { 

    beforeEach(function() { 
     // do this before each it EXCEPT 1.5 
    }); 
    it('1.1', function() { 

    }); 
    it('1.2', function() { 

    }); 
    it('1.3', function() { 

    }); 
    it('1.4', function() { 

    }); 
    }); 

    describe('only 5', function() { 
    it('1.5', function() { 
    // beforeEach shouldn't run before this 
    }); 

}); 

幕后描述将注册beforeEach功能,将调用所有itFunctions如果它存在。


选项2

功能将被依次调用,所以你也可以使用一个闭合时beforeEach获取运行控制 - 但它是一个有点哈克 - 如:

describe('1', function() { 
    var runBefore = true 
    beforeEach(function() { 
    // do this before each it EXCEPT 1.5 
    if (runBefore) { 
     // actual code 
    } 
    }); 
    // functions removed for brevity  
    it('1.4', function() { 
     runBefore = false; 
    }); 
    it('1.5', function() { 
    // beforeEach shouldn't run before this 

    // turn it back on for 1.6 
    runBefore = true; 
    }); 
});