描述()在摩卡咖啡中的作用是什么?

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

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。它仅仅是出于可读性的目的,还是这个函数还有其他用途?

我这样用有什么不对吗?

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

如果我这样做,测试仍然通过。

57532 次浏览

To my knowledge, describe is really just there for humans... So we can see different areas of the app. You can nest describe n levels deep.

describe('user',function(){
describe('create',function(){}
});

Describe is just used for the sake of understanding the purpose of the tests , it is also used to logically group the tests . Lets say you are testing the database API's , all the database tests could come under the outer describe , so the outer describe logically groups all the database related . Lets say there are 10 database related API's to test , each of the inner describe functions defines what those tests are ....

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 () {
});
});
});

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.

(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 #.)

Provide Banners

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)

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)

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)

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.

Provide Hooks

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:

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

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

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

  • 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.

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.

It's hard to add to Louis' excellent answer. There are a couple of advantages of the describe block that he didn't mention which are the skip and only functions.

describe.skip(...) {
...
}

will skip this describe and all its nested describe and it functions while:

describe.only(...) {
...
}

will only execute that describe and its nested describe and it functions. The skip() and only() modifiers can also be applied to the it() functions.

The particular role of describe is to indicate which component is being tested and which method of that component is also being tested.

for example, lets say we have a User Prototype

var User = function() {
const self = this;


function setName(name) {
self.name = name
}


function getName(name) {
return self.name;
}




return{setName, getName};
}


module.exports = User;

And it needs to be tested, so a spec file is created for unit test

var assert = require('assert');
var User = require("../controllers/user.controller");


describe("User", function() {
describe('setName', function() {
it("should set the name on user", function() {
const pedro = new User();


name = "Pedro"
pedro.setName(name);
assert(pedro.getName(), name);
});
});
});

It is easy to see that the purpose of describe is indicating the component to be tested and the nested describe methods indicate which methods needs to be tested