2014-09-18 132 views
2

目前我想了解BDD DSL如何在摩卡工作,我卡住了。我想要这种方法,并希望应用此。摩卡如何确定嵌套级别?

例如,下面的测试:

describe('foo', function(){ 
    describe('bar', function(){ 
     it('should be something') 
    }); 
}); 

将产生输出:

foo 
    bar 
    - should be something 


0 passing (4ms) 
1 pending 

问题:如何确定在嵌套块全局函数describe的调用为嵌套?我查看了源代码,但现在无法处理主要想法。

回答

1

摸查跟踪这些东西套房,你可以从source

/** 
* Describe a "suite" with the given `title` 
* and callback `fn` containing nested suites 
* and/or tests. 
*/ 

context.describe = context.context = function(title, fn){ 
    var suite = Suite.create(suites[0], title); 
    suite.file = file; 
    suites.unshift(suite); 
    fn.call(suite); 
    suites.shift(); 
    return suite; 
}; 

看到为了简化事情有点,每个describe,摩卡创建一个新的套件。套房可以包含其他套房。

对于您的示例,Mocha创建foo套件,其中包含bar套件,其中包含should be something测试。

0

它可以通过全局树数据结构来实现。

当你调用一个描述,摩卡添加一个节点,当你调用,摩卡加叶。

当根描述了调用返回时,树是完整的,并且摩卡一个接一个地遍历执行叶的节点。