单元测试Node.js和WebSockets(Socket.io) [英] Unit testing Node.js and WebSockets (Socket.io)

查看:166
本文介绍了单元测试Node.js和WebSockets(Socket.io)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以使用WebSockets(Socket.io)为Node.js提供坚如磐石,简陋的单元测试吗?

Could anyone provide a rock-solid, dead-simple unit test for Node.js using WebSockets (Socket.io)?

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

I'm using socket.io for Node.js, and have looked at socket.io-client for establishing the client connection to a server in the test. However, I seem to be missing something.

在下面的示例中,"worked ..."从未被打印出来.

In the example below, "worked..." never gets printed out.

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)

有什么建议吗?

推荐答案

经过进一步的戳戳和探查,我在

After further poking and prodding, I found some incredibly useful information at http://blog.foundry376.com/2012/09/connecting-to-a-socket-io-server-from-node-js-unit-tests. In the author's example, he points out the critical step of establishing socket listeners in the "before*" hooks. This example works (assuming a server is listening for socket connections at localhost:3001, of course)

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()放置在beforeEach,socket.on('connect'...)侦听器中对于建立连接至关重要.例如,如果在侦听器中注释掉done(),然后将其添加到一个作用域中(就在退出beforeEach之前),您将看到无法断开连接..."消息,而不是正在断开连接."消息. ."信息.像这样:

I found that the placement of done() in the beforeEach, socket.on('connect'...) listener was crucial to having the connection get established. For example, if you comment out done() in the listener, then add it one scope out (just before exiting the beforeEach), you'll see the "no connection to break..." message instead of the "disconnecting..." message. Like so:

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的新手,所以可能有一个很明显的理由要让启动程序将完成的()放置在套接字作用域本身中.希望这个小细节能省掉我其他人的头发.

I'm new to Mocha, so there's probably a very obvious reason to the initiated for placing done() withiin the socket scope itself. Hopefully that little detail will save others in my shoes from hair pulling.

对我来说,上面的测试(正确设置了done()的范围)输出:

For me, the above test (with correct scoping of done()) outputs:

  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)

这篇关于单元测试Node.js和WebSockets(Socket.io)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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