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
Post a Comment