Test-Driven Robotics

Test-Driven Robotics (TDR) is our nickname for applying the principles of Test-Driven-Development to hardware/software solutions.

Cylon.js, as an example of a modernized robotics development framework, has support for TDR built-in.

Here's an example robot "testbot.js":

var Cylon = require("cylon");

Cylon.robot({
  name: 'TestBot',

  connections: {
    arduino: { adaptor: 'firmata', port: '/dev/ttyACM0' }
  },

  devices: {
    led: { driver: 'led', pin: 13 }
  },

  work: function(my) {
    my.led.toggle();

    every((1).second(), function() {
      my.led.toggle();
    });
  }
}.start());

And here's a test "testbot_test.js", set up with a combination of the Mocha, Sinon, and Chai test frameworks:

"use strict";

process.env.NODE_ENV = 'test';

// setup for tests
var chai = require('chai'),
    sinon = require('sinon'),
    sinonChai = require('sinon-chai');

var expect = chai.expect;
chai.use(sinonChai);

var clock = sinon.useFakeTimers();

// load the robot, in test mode
var Cylon = require('cylon');

Cylon.config({ testMode: true })

require('./robot.js');

describe("TestBot", function() {
  var robot = Cylon.MCP.robots["TestBot"];

  it("should have work", function() {
    expect(robot.work).to.be.a('function');
  });

  it("should toggle the LED after one second", function() {
    var led = robot.led,
        toggle = sinon.stub(led, 'toggle');

    clock.tick(1000);
    expect(toggle).to.have.been.called;
  });
});

You would run the above test with the mocha command line, like this:

mocha testbot_test.js