Fuz*_*all 24 javascript mocha.js node.js
使用NodeJS和Mocha进行测试.我想我明白before()和beforeEach()是如何工作的.问题是,我想添加一个在每个"描述"之前而不是在每个"它"之前运行的设置脚本.
如果我使用before()它将只对整个套件运行一次,如果我使用beforeEach()它将在每次测试之前执行,所以我试图找到一个中间立场.
所以,如果这是我的测试文件:
require('./setupStuff');
describe('Suite one', function(){
it('S1 Test one', function(done){
...
});
it('S1 Test two', function(done){
...
});
});
describe('Suite two', function(){
it('S2 Test one', function(done){
...
});
});
Run Code Online (Sandbox Code Playgroud)
我想让"setupStuff"包含一个在"Suite one"和"Suite two"之前运行的函数
或者,换句话说,在"S1测试一个"和"S2测试一个"之前,但不是在"S1测试二"之前.
可以吗?
Lou*_*uis 21
没有与您想要的相似beforeEach或不同的电话before.但它不是必需的,因为你可以这样做:
function makeSuite(name, tests) {
describe(name, function () {
before(function () {
console.log("shared before");
});
tests();
after(function () {
console.log("shared after");
});
});
}
makeSuite('Suite one', function(){
it('S1 Test one', function(done){
done();
});
it('S1 Test two', function(done){
done();
});
});
makeSuite('Suite two', function(){
it('S2 Test one', function(done){
done();
});
});
Run Code Online (Sandbox Code Playgroud)
ya_*_*mon 10
你也可以用这种更灵活的方式做到这一点:
require('./setupStuff');
describe('Suite one', function(){
loadBeforeAndAfter(); //<-- added
it('S1 Test one', function(done){
...
});
it('S1 Test two', function(done){
...
});
});
describe('Suite two', function(){
loadBeforeAndAfter();//<-- added
it('S2 Test one', function(done){
...
});
});
describe('Suite three', function(){
//use some other loader here, before/after, or nothing
it('S3 Test one', function(done){
...
});
});
function loadBeforeAndAfter() {
before(function () {
console.log("shared before");
});
after(function () {
console.log("shared after");
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9380 次 |
| 最近记录: |