在测试期间将Mocha每次之前和之后 [英] Mocha beforeEach and afterEach during testing

查看:109
本文介绍了在测试期间将Mocha每次之前和之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用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.

如果您的测试套件更大或分布在多个文件中,我会非常强烈建议您将beforeEachafterEach以及it放在describe内,除非您绝对肯定套件中的每个测试都需要beforeEachafterEach完成的工作. (我已经用Mocha编写了多个测试套件,但我从来没有需要为每个测试运行的beforeEachafterEach.)类似:

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');
    });
});

如果您不这样将beforeEachafterEach放在describe内,那么假设您有一个文件可以测试Web套接字,而另一个文件可以测试某些数据库操作.包含数据库操作测试的文件中的测试 还将在它们之前和之后执行beforeEachafterEach.将beforeEachafterEach放在上述的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屋!

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