From 4dd7e013ed1b4077b4e07516fd40832470aaade9 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Fri, 11 Jul 2014 10:40:45 -0700 Subject: [PATCH] Require a connection if a robot has any devices --- lib/robot.js | 7 +++++++ test/specs/robot.spec.js | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/robot.js b/lib/robot.js index 5786928..36c9769 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -89,6 +89,13 @@ var Robot = module.exports = function Robot(opts) { this.initConnections(opts.connection || opts.connections); this.initDevices(opts.device || opts.devices); + var hasDevices = !!Object.keys(this.devices).length, + hasConnections = !!Object.keys(this.connections).length; + + if (hasDevices && !hasConnections) { + throw new Error("No connections specified"); + } + for (var n in opts) { var func = opts[n], reserved = ['connection', 'connections', 'device', 'devices', 'work']; diff --git a/test/specs/robot.spec.js b/test/specs/robot.spec.js index 6984124..b83995b 100644 --- a/test/specs/robot.spec.js +++ b/test/specs/robot.spec.js @@ -71,6 +71,19 @@ describe("Robot", function() { expect(robot.extraFunction).to.be.eql(extraFunction); expect(robot.extraValue).to.be.eql("Hello World"); }); + + context("if there are devices but no connections", function() { + it('throws an error', function() { + var fn = function() { + return new Robot({ + name: 'BrokenBot', + device: { name: 'ping', driver: 'ping' } + }); + }; + + expect(fn).to.throw(Error, "No connections specified"); + }); + }); }); describe("all work and no play", function() { @@ -229,6 +242,7 @@ describe("Robot", function() { beforeEach(function() { bot = new Robot({ + connection: [{ name: 'loopback', adaptor: 'loopback' }], devices: [ { name: 'alpha', driver: 'ping' }, { name: 'bravo', driver: 'ping' }