jeudi 3 septembre 2015

Mocking Websocket Server for Unit Testing

I am using sockJS on the client side for the websockets implementation of my project (the socket server is run by a Java Spring instance). I'm having trouble figuring out how to mock the websockets server for my front end sockets unit testing.

I have some functions that depend on the data being streamed from the server. But I can't figure out how to mock this data such that sock.onmessage is called.

My client is setup in general like this:

--- websocket-client.js (sockJS) ---

        sock = new SockJS('/server/uri');

        sock.onopen = function() {

        };

        sock.onmessage = function(e) {
          // do stuff with e data
        };

        sock.onclose = function() {
          // clean up stuff
        };

I'm using Karma and Chai for my test runner/assertion library.

Since this is an Angular application, usually I can mock my API services using $httpBackend. But I don't think this is an option in this case.

Aucun commentaire:

Enregistrer un commentaire