How to stub a web socket (supplying only an event-like object as an object) in javascript

I'm looking for the easiest way to stub in a custom event emitter when creating a socket. I understand it can be done w / sinon (as shown below)

var socket = new io.EventEmitter();
sinon.stub(io, 'connect').returns(socket);
// initialization that causes io.connect to be invoked
socket.emit('connect');

      

Ideally, I am looking for what I see as a real "black box" that knows nothing about the underlying technology. I do this w / ajax with a simple library called fauxjax today for xhrs, and it allows me to stub "return this json when this url is a request via http GET". If I decide to switch from jQuery $ .ajax to another xhr library, then there is no need to reprocess the test because I wrote them a technology agnostic.

I'm looking for this same "agnostic patch cable" for Websockets - is there anything like this? Or is a complete stub / spy / mock library like sinon the only approach worth looking at today?

I'm leading the way with a simple window.WebSocket monkey patch - just wanted to see if others had such luck.

+3


source to share





All Articles