在测试期间将Mocha每次之前和之后 [英] Mocha beforeEach and afterEach during testing
问题描述
我一直在尝试使用mocha测试我的测试服务器.这是我使用的以下代码,与另一篇类似文章中的代码几乎相同.
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.
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');
});
问题是,当我执行此草稿时,在命令提示符下看不到预期会看到的console.log.你能告诉我我做错了什么吗?
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?
推荐答案
Georgi是正确的,您需要调用it
来指定测试,但是如果文件中不需要顶级describe
你不想.您可以用一堆it
调用替换单个describe
:
Georgi is correct that you need an it
call to specify a test but you don't need to have a top level describe
in your file if you don't want to. You could replace your single describe
with a bunch of it
calls:
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.
如果您的测试套件更大或分布在多个文件中,我会非常强烈建议您将beforeEach
和afterEach
以及it
放在describe
内,除非您绝对肯定套件中的每个测试都需要beforeEach
或afterEach
完成的工作. (我已经用Mocha编写了多个测试套件,但我从来没有需要为每个测试运行的beforeEach
或afterEach
.)类似:
If your test suite is bigger or spread among multiple files, I would very strongly suggest you put your beforeEach
and afterEach
together with your it
inside the describe
, unless you are absolutely positive that every single test in the suite needs the work done by beforeEach
or afterEach
. (I've written multiple test suites with Mocha and I've never had a beforeEach
or afterEach
that I needed to run for every single test.) Something like:
describe('WebSocket test', function() {
beforeEach(function(done) {
// ...
});
afterEach(function(done) {
// ...
});
it('response should be null', function() {
assert.equal(response.result, null, 'Successful Authentification');
});
});
如果您不这样将beforeEach
和afterEach
放在describe
内,那么假设您有一个文件可以测试Web套接字,而另一个文件可以测试某些数据库操作.包含数据库操作测试的文件中的测试 还将在它们之前和之后执行beforeEach
和afterEach
.将beforeEach
和afterEach
放在上述的describe
内,将确保仅在您的Web套接字测试中执行它们.
If you do not put your beforeEach
and afterEach
inside describe
like 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 also have your beforeEach
and afterEach
executed before and after them. Putting the beforeEach
and afterEach
inside the describe
like shown above will ensure that they are performed only for your web socket tests.
这篇关于在测试期间将Mocha每次之前和之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!