javascript - Mocking/Unittesting when using Node serialport callbacks -


i working on web-interface 3rd party device connects via serial port. example code below...

var serialport = require("serialport"); var serialport = serialport.serialport; var sp = new serialport("/dev/ttyacm0", {     parser: serialport.parsers.readline("\n") });  sp.on("data", function (rawdata) { ... 

my question how can mock/unittest sp "data" event...

you can emit 'data' event @ because serialport implements the eventemitter interface. use .emit().

this code log "woot" console though serial interface didn't receive data. fakes .emit().

var serialport = require("serialport"); var serialport = serialport.serialport; var sp = new serialport("/dev/ttys000", {     parser: serialport.parsers.readline("\n") });  sp.on("data", function (rawdata) {     console.log('woot'); });  sp.emit('data'); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -