node.js 摩卡中describe()的作用是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19298118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the role of describe() in Mocha?
提问by Ritesh Mehandiratta
The documentation at the official Mocha sitecontains this example:
Mocha 官方网站上的文档包含以下示例:
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();
});
})
})
})
I want to know when I should nest my tests in the describefunction and what the basic purpose of describeis. Can I compare the first argument passed to describeto comments in a programming language? Nothing is shown of describein the output on the console. Is it only for readability purposes, or there is some other use for this function?
我想知道什么时候应该在describe函数中嵌套我的测试以及它的基本目的describe是什么。我可以将传递给的第一个参数与describe编程语言中的注释进行比较吗?describe控制台的输出中没有显示任何内容。是仅出于可读性目的,还是此功能还有其他用途?
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.
如果我这样做,测试仍然通过。
回答by Louis
The itcall identifies each individual tests but by itself itdoes not tell Mocha anything about how your test suite is structured. How you use the describecall is what gives structure to your test suite. Here are some of the things that using describeto structure your test suite does for you. Here's an example of a test suite, simplified for the purpose of discussion:
该it调用标识了每个单独的测试,但它本身it并没有告诉 Mocha 任何有关您的测试套件的结构的信息。您如何使用describe调用决定了您的测试套件的结构。以下是describe用于构建测试套件的一些功能。这是一个测试套件的例子,为了讨论的目的而简化:
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 Fooand Barare full-fledged classes. Foohas cloneand equalsmethods. Barhas clone. The structure I have above is one possible way to structure tests for these classes.
试想一下,Foo并Bar有全面的课程。Foo有clone和equals方法。Bar有clone. 我上面的结构是为这些类构建测试的一种可能方法。
(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 #.)
(#某些系统(例如 jsdoc)使用该符号表示实例字段。因此,当与方法名称一起使用时,它表示在类的实例上调用的方法(而不是在类的实例上调用的方法)在类本身上)。测试套件在没有 的情况下也能运行#。)
Provide Banners
提供横幅
Some of Mocha's reporters show the names you give to describein the reports they produce. For instance, the specreporter (which you can use by running $ mocha -R spec), would report:
Mocha 的一些记者describe在他们制作的报告中显示了您的名字。例如,spec报告器(您可以通过运行使用它$ mocha -R spec)将报告:
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 --grepoption. So if you care only about the Barclass, you can do $ mocha -R spec --grep Bar, and get the output:
如果您只想运行某些测试,则可以使用该--grep选项。因此,如果您只关心Bar课程,则可以执行$ mocha -R spec --grep Bar,并获得输出:
Bar
#clone
? clones the object
1 passing (4ms)
Or if you care only about the clonemethods of all classes, then $ mocha -R spec --grep '\bclone\b'and get the output:
或者,如果您只关心clone所有类的方法,则$ mocha -R spec --grep '\bclone\b'获取输出:
Foo
#clone
? clones the object
Bar
#clone
? clones the object
2 passing (5ms)
The value given to --grepis interpreted as a regex so when I pass \bclone\bI'm asking only for the word clone, and not things like clonesor cloned.
给定的值--grep被解释为一个正则表达式,所以当我通过时,我\bclone\b只要求这个词clone,而不是像clonesor 之类的东西cloned。
Provide Hooks
提供挂钩
In the example above the beforeEachand afterEachcalls are hooks. Each hook affects the itcalls that are inside the describecall which is the parent of the hook. The various hooks are:
在上面的例子中,beforeEach和afterEach调用是钩子。每个钩子都会影响作为钩子父级it的describe调用内部的调用。各种钩子是:
beforeEachwhich runs before each individualitinside thedescribecall.afterEachwhich runs after each individualitinside thedescribecall.beforewhich runs once before any of the individualitinside thedescribecall is run.afterwhich runs once after all the individualitinside thedescribecall are run.
beforeEach它it在describe呼叫中的每个人之前运行。afterEach它it在describe呼叫中的每个人之后运行。beforeit在describe调用中的任何个人运行之前运行一次。afterit在describe调用中的所有个体运行后运行一次。
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.
回答by Zeke Alexandre Nierenberg
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 真的只适用于人类......所以我们可以看到应用程序的不同区域。您可以嵌套描述 n 级深。
describe('user',function(){
describe('create',function(){}
});
回答by Guy
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 skipand onlyfunctions.
很难补充路易斯的出色答案。describe 块有几个他没有提到的优点,即skip和only函数。
describe.skip(...) {
...
}
will skip this describe and all its nested describe and it functions while:
将跳过此 describe 及其所有嵌套的 describe 并在以下情况下运行:
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.
只会执行该 describe 及其嵌套的 describe 和它的功能。的skip()和only()改性剂也可以被施加到它()函数。
回答by Karthic Rao
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 ....
Describe 仅用于理解测试的目的,也用于对测试进行逻辑分组。假设您正在测试数据库 API,所有数据库测试都可以在外部 describe 下进行,因此外部 describe 在逻辑上将所有与数据库相关的 . 假设有 10 个与数据库相关的 API 需要测试,每个内部描述函数都定义了这些测试是什么......
回答by Pedro Machado
The particular role of describe is to indicate which component is being tested and which method of that component is also being tested.
describe 的特殊作用是指示正在测试哪个组件以及正在测试该组件的哪种方法。
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
并且需要测试,所以创建了一个spec文件用于单元测试
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
不难看出,describe的目的是指明要测试的组件,嵌套的describe方法指明需要测试哪些方法

