node.js 摩卡 beforeEach 和 afterEach 测试期间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22762301/
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
Mocha beforeEach and afterEach during testing
提问by zamponotyropita
I have been trying to test my test server using mocha. This is the following code that I use, almost the same as one found in another similar post.
我一直在尝试使用 mocha 测试我的测试服务器。这是我使用的以下代码,几乎与在另一篇类似帖子中找到的代码相同。
beforeEach(function(done) {
// Setup
console.log('test before function');
ws.on('open', function() {
console.log('worked...');
done();
});
ws.on('close', function() {
console.log('disconnected...');
});
});
afterEach(function(done) {
// Cleanup
if(readyState) {
console.log('disconnecting...');
ws.close();
} else {
// There will not be a connection unless you have done() in beforeEach, socket.on('connect'...)
console.log('no connection to break...');
}
done();
});
describe('WebSocket test', function() {
//assert.equal(response.result, null, 'Successful Authentification');
});
the problem is that when I execute this draft, none of the console.log that is expected to be seen is visible on the command prompt. Can you please explain to me what am I doing wrong?
问题是,当我执行此草稿时,在命令提示符上看不到任何预期会看到的 console.log。你能向我解释一下我做错了什么吗?
回答by Louis
Georgi is correct that you need an itcall to specify a test but you don't need to have a top level describein your file if you don't want to. You could replace your single describewith a bunch of itcalls:
Georgi 是正确的,您需要it调用来指定测试,但describe如果您不想,则不需要在文件中包含顶级。你可以用describe一堆it电话替换你的单曲:
it("first", function () {
// Whatever test.
});
it("second", function () {
// Whatever other test.
});
This works very well if your test suite is small and composed of only one file.
如果您的测试套件很小且仅由一个文件组成,则此方法非常有效。
If your test suite is bigger or spread among multiple files, I would very stronglysuggest you put your beforeEachand afterEachtogether with your itinside the describe, unless you are absolutely positive that every single test in the suite needs the work done by beforeEachor afterEach. (I've written multiple test suites with Mocha and I've never had a beforeEachor afterEachthat I needed to run for every single test.) Something like:
如果您的测试套件更大或分布在多个文件中,我强烈建议您将您的beforeEach和afterEach与您的it放在一起describe,除非您绝对肯定套件中的每个测试都需要由beforeEach或完成的工作afterEach。(我已经用 Mocha 编写了多个测试套件,但我从来没有为每个测试运行过一个beforeEach或afterEach一个。)类似于:
describe('WebSocket test', function() {
beforeEach(function(done) {
// ...
});
afterEach(function(done) {
// ...
});
it('response should be null', function() {
assert.equal(response.result, null, 'Successful Authentification');
});
});
If you do not put your beforeEachand afterEachinside describelike this, then let's say you have one file to test web sockets and another file to test some database operations. The tests in the file that contains the database operation tests will alsohave your beforeEachand afterEachexecuted before and after them. Putting the beforeEachand afterEachinside the describelike shown above will ensure that they are performed only for your web socket tests.
如果你没有像这样把你的beforeEachandafterEach放在里面describe,那么假设你有一个文件来测试 web sockets 和另一个文件来测试一些数据库操作。包含数据库运行试验,将文件中的测试也有你beforeEach和afterEach前,后执行它们。将beforeEach和afterEach放在describe上面显示的类似内容中将确保它们仅用于您的网络套接字测试。
回答by Georgi
You have no tests in your example. If there are no tests to be run, then before and after hooks won't be invoked. Try adding a test like:
您的示例中没有测试。如果没有要运行的测试,则不会调用 before 和 after 钩子。尝试添加如下测试:
describe('WebSocket test', function() {
it('should run test and invoke hooks', function(done) {
assert.equal(1,1);
done();
});
});
回答by OmmyJay
You need to have a test-callback(eg. it) inside suite-callback(eg, describe) to execute beforeEach()and afterEach()hooks. More info https://mochajs.org/#run-cycle-overview
您需要it在 suite-callback(eg, describe)内有一个 test-callback(eg. ) 来执行beforeEach()和afterEach()挂钩。更多信息https://mochajs.org/#run-cycle-overview

