Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing socket.io events

I'm working on a nodejs application. I managed to break some of the code in separate modules, that I'm testing using nodeunit, but a substantial part of my code base still relies on socket.io.

How can I test my socket.io events?

like image 978
vise Avatar asked Oct 06 '11 13:10

vise


1 Answers

Try this article: Socket.IO and Asynchronous Testing with node.js.

So, let's fake. I mean, let's create fake instances of the contender servers, as well as the visualization client. Then, we'll build a test suite that uses these fakes to test-drive the actual server we're developing. I'll paste some slightly simplified pieces of code here for example. If you are really interested in the stuff, you'll find it all on Github.

edit: if you need to test functionality for which there is no immediate output, Jasmine BDD provides spies. These can be used to "see inside" a function and test whether certain parameters are passed etc, and are useful for mocking asynchronous calls for testing, as well as writing more functional tests:

Jasmine Spies are test doubles that can act as stubs, spies, fakes or when used in an expecation, mocks...Spies can be checked if they were called or not and what the calling params were. A Spy has the following fields: wasCalled, callCount, mostRecentCall, and argsForCall (see docs). Spies are torn down at the end of every spec.

Docs, an article and a related SO question.

like image 87
Andy Avatar answered Sep 28 '22 02:09

Andy