在Mocha中describe()的作用是什么? [英] What is the role of describe() in Mocha?

查看:145
本文介绍了在Mocha中describe()的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摩卡官方网站上的文档包含以下示例:

The documentation at the official Mocha site contains this example:

describe('User', function(){
  describe('#save()', function(){
    it('should save without error', function(done){
      var user = new User('Luna');
      user.save(function(err){
        if (err) throw err;
        done();
      });
    })
  })
})

我想知道什么时候应该将测试嵌套在describe函数中,以及describe的基本用途是什么.我可以将传递给describe的第一个参数与编程语言中的注释进行比较吗?在控制台的输出中没有显示describe的任何内容.是仅出于可读性目的,还是该功能还有其他用途?

I want to know when I should nest my tests in the describe function and what the basic purpose of describe is. Can I compare the first argument passed to describe to comments in a programming language? Nothing is shown of describe in the output on the console. Is it only for readability purposes, or there is some other use for this function?

如果我这样使用它有什么问题吗?

Is there anything wrong if I use it like this?

describe('User', function(){
    describe('#save()', function(){
        var user = new User('Luna');
        user.save(function(err){
            if (err) throw err;
            done();
        })
    })
})

如果我这样做,则测试仍会通过.

If I do it this way, the test still passes.

推荐答案

it调用标识每个单独的测试,但是it本身不告诉Mocha任何有关您的测试套件如何结构化的信息. >.使用describe调用的方式为测试套件提供了结构.以下是使用describe构造测试套件的一些操作.这是一个测试套件的示例,出于讨论目的对其进行了简化:

The it call identifies each individual tests but by itself it does not tell Mocha anything about how your test suite is structured. How you use the describe call is what gives structure to your test suite. Here are some of the things that using describe to structure your test suite does for you. Here's an example of a test suite, simplified for the purpose of discussion:

function Foo() {
}

describe("Foo", function () {
    var foo;
    beforeEach(function () {
        foo = new Foo();
    });
    describe("#clone", function () {
        beforeEach(function () {
            // Some other hook
        });
        it("clones the object", function () {
        });
    });
    describe("#equals", function () {
        it("returns true when the object passed is the same", function () {
        });
        it("returns false, when...", function () {
        });
    });
    afterEach(function () {
        // Destroy the foo that was created.
        // foo.destroy();
    });
});

function Bar() {
}

describe("Bar", function () {
    describe("#clone", function () {
        it("clones the object", function () {
        });
    });
});

想象一下FooBar是成熟的类. Foo具有cloneequals方法. Bar具有clone.我上面的结构是为这些类构造测试的一种可能方法.

Imagine that Foo and Bar are full-fledged classes. Foo has clone and equals methods. Bar has clone. The structure I have above is one possible way to structure tests for these classes.

(#表示法在某些系统(例如jsdoc)中用于表示实例字段.因此,当与方法名称一起使用时,它表示在类的实例上调用的方法(而不是类方法,该方法将在类本身上调用.)在没有#的情况下,测试套件也可以正常运行.)

(The # notation is used by some systems (like for instance, jsdoc) to indicate an instance field. So when used with a method name, it indicates a method called on an instance of the class (rather than a class method, which is called on the class itself). The test suite would run just as well without the presence of #.)

某些Mocha的记者会在其生成的报告中显示您为describe指定的名称.例如,spec报告程序(您可以通过运行$ mocha -R spec使用该报告程序)将报告:

Some of Mocha's reporters show the names you give to describe in the reports they produce. For instance, the spec reporter (which you can use by running $ mocha -R spec), would report:

  Foo
    #clone
      ✓ clones the object 
    #equals
      ✓ returns true when the object passed is the same 
      ✓ returns false, when... 

  Bar
    #clone
      ✓ clones the object 


  4 passing (4ms)

帮助选择要运行的零件

如果只想运行某些测试,则可以使用--grep选项.因此,如果只关心Bar类,则可以执行$ mocha -R spec --grep Bar并获取输出:

Help Select Parts to Run

If you want to run only some of the tests, you can use the --grep option. So if you care only about the Bar class, you can do $ mocha -R spec --grep Bar, and get the output:

  Bar
    #clone
      ✓ clones the object 


  1 passing (4ms)

或者如果您只关心所有类的clone方法,则$ mocha -R spec --grep '\bclone\b'并获取输出:

Or if you care only about the clone methods of all classes, then $ mocha -R spec --grep '\bclone\b' and get the output:

  Foo
    #clone
      ✓ clones the object 

  Bar
    #clone
      ✓ clones the object 


  2 passing (5ms)

赋予--grep的值被解释为正则表达式,因此当我通过\bclone\b时,我只要求输入单词clone,而不是诸如clonescloned之类的东西.

The value given to --grep is interpreted as a regex so when I pass \bclone\b I'm asking only for the word clone, and not things like clones or cloned.

在上面的示例中,beforeEachafterEach调用是钩子.每个挂钩都会影响describe调用内部的it调用,该调用是该挂钩的父级.各种钩子是:

In the example above the beforeEach and afterEach calls are hooks. Each hook affects the it calls that are inside the describe call which is the parent of the hook. The various hooks are:

  • beforeEachdescribe调用中每个单独的it之前运行.

  • beforeEach which runs before each individual it inside the describe call.

afterEachdescribe调用中的每个it之后运行.

afterEach which runs after each individual it inside the describe call.

before,它在describe调用中的任何单个it运行之前运行一次.

before which runs once before any of the individual it inside the describe call is run.

after会在describe调用中的所有单个it运行之后运行一次.

after which runs once after all the individual it inside the describe call are run.

这些挂钩可用于获取测试所需的资源或创建测试所需的数据结构,然后在测试完成后释放资源或销毁这些结构(如果需要).

These hooks can be used to acquire resources or create data structures needed for the tests and then release resources or destroy these structures (if needed) after the tests are done.

您在问题末尾显示的摘录不会产生错误,但实际上不包含任何测试,因为测试是由it定义的.

The snippet you show at the end of your question won't generate an error but it does not actually contain any test, because tests are defined by it.

这篇关于在Mocha中describe()的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆