Unittesting and mocking nodeJS net / Socket events

I have a TDD'ing script that connects to Socket from the net module.

For testing I use mocha and for sinonjs ridicule

Now I really have the problem of bullying / falsifying the emission of socket data.

For example: I want to check this code:

 socket.on('data', function (data) { console.log(data); } 

How can I fake "data"?

+4
source share
1 answer

You need to set the socket variable in some way. I will need to see more of your code to recommend a better way to do this.

And then in your test, you can call socket.emit() .

0
source

Source: https://habr.com/ru/post/1446546/


All Articles