vendredi 25 mars 2016

Mocha testing with events

I'm working with the thermal printer library that has a few on ready events. I'm pretty new to TDD so I'm having a hard time figure out how to test the code below.

How do set up a test so that I can make sure all of those chained functions got called compounded by the fact that it's all within an onready event?

var SerialPort = require('serialport').SerialPort,
    serialPort = new SerialPort('/dev/ttyUSB0', {
        baudrate: 19200
    }),
    Printer = require('thermalprinter');

var path = __dirname + '/images/nodebot.png';

serialPort.on('open',function() {
    var printer = new Printer(serialPort);
    printer.on('ready', function() {

        console.log('asdf');
        printer
            .indent(10)
            .horizontalLine(16)
            .bold(true)
            .indent(10)
            .printLine('first line')
            .bold(false)
            .inverse(true)
            .big(true)
            .right()
            .printLine('second line')
            .printImage(path)
            .print(function() {
                console.log('done');
                process.exit();
            });
    });
});

Aucun commentaire:

Enregistrer un commentaire