目前,我正在尝试了解BDD DSL在Mocha中的工作方式,我感到困惑。我想要这种方法,并想应用这种方法。

例如,以下测试:

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


将产生输出:

foo
  bar
    - should be something


0 passing (4ms)
1 pending


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

最佳答案

source中可以看到,Mocha在Suites中跟踪这些事情。

/**
 * 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;
};


为了简化一点,Mocha为每个describe创建了一个新套件。套房可以包含其他套房。

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

关于javascript - Mocha 如何确定嵌套级别?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25923750/

10-12 12:34
查看更多