Test drive your bus.io apps with bus.io-driver
.
var Message = require('bus.io-common').Message;
var driver = require('bus.io-driver');
var bus = require('bus.io')();
bus.on(function (msg, next) {
msg.content(msg.content().toUpperCase());
});
driver(bus)
.on(Message().content('hi'))
.done(function (err, msg) {
if (err) throw err;
assert.equal(msg.content(), 'HI');
});
Say you want to test this bus.io app.
var bus = require('bus.io')();
bus.in('shout', function (msg, sock) {
msg.target('everyone').deliver();
});
bus.on('shout', function (msg) {
msg.content(msg.content().toUpperCase()).deliver();
});
bus.out('shout', function (msg) {
msg.content(msg.content() + '!!!').deliver();
});
You would do it like this with driver.
var driver = require('bus.io-driver');
var assert = require('assert');
var Message = require('bus.io-common').Message;
var bus = require('./my-bus.io-app.js');
// test the "shout" message handler when going from the socekt to the bus
driver(bus)
.on(Message().action('shout').content('hi'))
.done(function (err, mesage) {
if (err) throw err;
assert.equal(msg.target(), 'everyone');
});
// test the "shout" message handler when processing on the bus
driver(bus)
.on(Message().action('shout').content('hi'))
.done(function (err, ms) {
if (err) throw err;
assert.equal(msg.content(), 'HI');
});
// test the "shout" message handler when going from the bus to the socket
driver(bus)
.on(Message().action('shout').content('HI'))
.done(function (err, msg) {
if (err) throw err;
assert.equal(msg.content(), 'HI!!!');
});
Test the whole trip.
var msg = Message().action('shout').content('hi');
driver(bus)
.in(msg).on(msg).out(msg)
.done(function (err, msg) {
if (err) throw err;
assert.equal(msg.target(), 'everyone');
assert.equal(message.content(), 'HI!!!');
});
Install node.js (See download and install instructions here: http://nodejs.org/).
Clone this repository
> git clone [email protected]:turbonetix/bus.io-driver.git
cd into the directory and install the dependencies
> cd bus.io-driver
> npm install && npm shrinkwrap --dev
var driver = require('bus.io-driver');
var bus = require('./bus.js');
var instance = driver(bus);
var events = require('events');
var sock = new events.EventEmitter;
var driver = require('bus.io-driver');
var bus = require('./bus.js');
var instance = driver(bus, sock);
Pipes the message through the in()
receiver stack of middleware.
driver(bus).in(Message().action('shout'));
Pipes the message through the on()
receiver stack of middleware.
driver(bus).on(Message().action('shout'));
Pipes the message through the out()
receiver stack of middleware.
driver(bus).out(Message().action('shout'));
driver.done(function (err, msg) {
if (err) throw nerr;
assert.equal(msg.content(), 'HI!!!');
});
Install coffee-script
> npm install coffee-script -g
Tests are run using grunt. You must first globally install the grunt-cli with npm.
> sudo npm install -g grunt-cli
To run the tests, just run grunt
> grunt spec
Examples are under the examples/
directory.