一尘不染

单元测试Node.js和WebSockets(Socket.io)

node.js

谁能使用WebSockets(Socket.io)为Node.js提供坚如磐石,简单的单元测试?

我使用用于Node.js的socket.io,并查看了socket.io-client以在测试中建立与服务器的客户端连接。但是,我似乎缺少了一些东西。

在下面的示例中,“ worked …”从未被打印出来。

var io = require('socket.io-client')
, assert = require('assert')
, expect = require('expect.js');

describe('Suite of unit tests', function() {

    describe('First (hopefully useful) test', function() {

        var socket = io.connect('http://localhost:3001');
        socket.on('connect', function(done) {
            console.log('worked...');
            done();
        });

        it('Doing some things with indexOf()', function() {
            expect([1, 2, 3].indexOf(5)).to.be.equal(-1);
            expect([1, 2, 3].indexOf(0)).to.be.equal(-1);
        });

    });
});

相反,我只是得到:

  Suite of unit tests
    First (hopefully useful) test
      ✓ Doing some things with indexOf()


  1 test complete (26 ms)

有什么建议?


阅读 264

收藏
2020-07-07

共1个答案

一尘不染

经过进一步的摸索和探索,我发现了一些非常有用的信息。在作者的示例中,他指出了在 before
钩子中建立套接字侦听器的关键步骤。

此示例有效:

假设一个服务器监听的套接字连接localhost:3001,当然

var io = require('socket.io-client')
, assert = require('assert')
, expect = require('expect.js');

describe('Suite of unit tests', function() {

    var socket;

    beforeEach(function(done) {
        // Setup
        socket = io.connect('http://localhost:3001', {
            'reconnection delay' : 0
            , 'reopen delay' : 0
            , 'force new connection' : true
        });
        socket.on('connect', function() {
            console.log('worked...');
            done();
        });
        socket.on('disconnect', function() {
            console.log('disconnected...');
        })
    });

    afterEach(function(done) {
        // Cleanup
        if(socket.connected) {
            console.log('disconnecting...');
            socket.disconnect();
        } else {
            // There will not be a connection unless you have done() in beforeEach, socket.on('connect'...)
            console.log('no connection to break...');
        }
        done();
    });

    describe('First (hopefully useful) test', function() {

        it('Doing some things with indexOf()', function(done) {
            expect([1, 2, 3].indexOf(5)).to.be.equal(-1);
            expect([1, 2, 3].indexOf(0)).to.be.equal(-1);
            done();
        });

        it('Doing something else with indexOf()', function(done) {
            expect([1, 2, 3].indexOf(5)).to.be.equal(-1);
            expect([1, 2, 3].indexOf(0)).to.be.equal(-1);
            done();
        });

    });

});

我发现的位置done()beforeEachsocket.on('connect'...)监听器是具有连接得到建立至关重要。例如,如果您done()在侦听器中将其注释掉,然后将其添加到一个作用域中(
就在退出之前beforeEach),则会看到 “ no connection to break …” 消息,而不是
disconnecting …”
消息。像这样:

beforeEach(function(done) {
    // Setup
    socket = io.connect('http://localhost:3001', {
        'reconnection delay' : 0
        , 'reopen delay' : 0
        , 'force new connection' : true
    });
    socket.on('connect', function() {
        console.log('worked...');
        //done();
    });
    socket.on('disconnect', function() {
        console.log('disconnected...');
    });
    done();
});

我是Mocha的新手,因此可能有一个很明显的原因要让启动程序放置done()在套接字作用域本身内。希望这个小细节可以省掉我的鞋子中的其他头发。

对我来说,上述测试( 正确设定范围done())输出:

  Suite of unit tests
    First (hopefully useful) test
      ◦ Doing some things with indexOf(): worked...
      ✓ Doing some things with indexOf() 
disconnecting...
disconnected...
      ◦ Doing something else with indexOf(): worked...
      ✓ Doing something else with indexOf() 
disconnecting...
disconnected...


  2 tests complete (93 ms)
2020-07-07