admin管理员组

文章数量:1427340

I have tried to do unit testing for a web socket application using sinon.js,

One of the users on github of sinon, did this, but I am not able to understand how it does help to unit test websocket applications for validating the received data which was sent to fake server.

var dummySocket = { send : sinon.spy()};
sinon.stub(window, 'WebSocket').returns(dummySocket);
dummySocket = new WebSocket('ws://html5rocks.websocket/echo');
dummySocket.onopen();
dummySocket.onmessage(JSON.stringify({ hello : 'from server' }));
// You can assert whether your code sent something to the server like this:
sinon.assert.calledWith(dummySocket.send, '{"the client":"says hi"}');

My questions are

  • How can I receive the same data from fake server which have been sent to server earlier.
  • How can I send data to fake server by using send method of fake socket object(eg:- socket.send())?
  • How can I get data from server on dummySocket.onmessage = function (msg){}

With sinon.js, I could not get the any process to create fake websocket object like for fake XMLHttpRequest and server by using respectively useFakeXMLHttpRequest() and fakeServer.create()

Is there any process to achieve this on sinon.js?

I have tried to do unit testing for a web socket application using sinon.js,

One of the users on github of sinon, did this, but I am not able to understand how it does help to unit test websocket applications for validating the received data which was sent to fake server.

var dummySocket = { send : sinon.spy()};
sinon.stub(window, 'WebSocket').returns(dummySocket);
dummySocket = new WebSocket('ws://html5rocks.websocket/echo');
dummySocket.onopen();
dummySocket.onmessage(JSON.stringify({ hello : 'from server' }));
// You can assert whether your code sent something to the server like this:
sinon.assert.calledWith(dummySocket.send, '{"the client":"says hi"}');

My questions are

  • How can I receive the same data from fake server which have been sent to server earlier.
  • How can I send data to fake server by using send method of fake socket object(eg:- socket.send())?
  • How can I get data from server on dummySocket.onmessage = function (msg){}

With sinon.js, I could not get the any process to create fake websocket object like for fake XMLHttpRequest and server by using respectively useFakeXMLHttpRequest() and fakeServer.create()

Is there any process to achieve this on sinon.js?

Share Improve this question edited Nov 5, 2015 at 6:10 Krupesh Kotecha 2,4123 gold badges22 silver badges40 bronze badges asked Apr 10, 2014 at 18:21 Suman BogatiSuman Bogati 6,3512 gold badges24 silver badges36 bronze badges 5
  • The github user suggested something and clearly stated he doesn't know if it even works (Hint: It doesn't). – Amit Commented May 15, 2015 at 8:19
  • Have you ever figured this out? if so, Please post your findings – Mawaheb Commented Jul 15, 2015 at 11:39
  • I don't know if this might help you, but I wrote a simple npm module that acts as a wrapper for a socket.io server. When testing this module I didn't use sinon, but the socket.io Client to send/receive data for testing purposes. If this is what you need, I could elaborate my approach in an answer. – danillouz Commented Aug 2, 2015 at 12:44
  • 4 You might want to take a look at mock-socket, it seems to do what you're wanting: github./thoov/mock-socket – Michael Bleigh Commented Aug 30, 2015 at 7:29
  • 1 You could a fake XMLHttpRequest to imitate the Socket request and when it returns it calls onmessage. But this wont test the actual web socket connection but rather the web socket flow. So in the case you want to test the actual connection, something like what @MichaelBleigh suggested might do the trick. – Tokimon Commented Nov 14, 2015 at 22:44
Add a ment  | 

1 Answer 1

Reset to default 2

Normally, you would do ws = sinon.createStubInstance(WebSocket), but this isn't possible since properties on the WebSocket.prototype throw exceptions when reading them. There are two ways around this.

  1. You could add a useFakeWebSocket to sinon to overwrite WebSocket. This would be similar to what useFakeXMLHttpRequest does to XMLHttpRequest.
  2. Duck type out a WebSocket object by iterating over the prototype.

    beforeEach(function () {
        var ws = {};
        for (var prop in WebSocket.prototype) {
            ws[prop] = function () {}; // some properties aren't functions.
        }
    });
    

If you wanted to implement a mock echo WebSocket server so that you can test your event handlers, you could do that with this:

var ws;
beforeEach(function () {
    ws = {
        send: function (msg) {
            this.onmessage({ data: msg });
        },
        onmessage: function (e) {
            // stub
        }
    };
});
it('should echo', function () {
    var spy = sinon.spy(ws, 'onmessage');
    ws.send('this is a test');
    assertEquals(spy.args[0][0].data, 'this is a test');
});

I hope this is what you're looking for.

本文标签: javascriptUnit testing of websocket application by sinonjsStack Overflow