moat

MOAT js Testing Library for Node.js

MOAT js Testing Library

This is a node.js library enabling you to run MOAT js script files on your local machine rather than a cloud server.

MOAT js is a javascript API to build a server side application interacting with remote devices such as OMA-DM based devices, ZigBee devices and/or Android devices. It is a part of MOAT IoT, which is a specification set of creating IoT applications running on Inventit® Service-Sync environment.


The library offers you:

  • to create and run the unit test cases for your MOAT js scripts without deployment

You can deploy a script depending on this library to a cloud server AS IT IS. This is because Inventit® Service-Sync runtime automcatically ignores the 'require' statement.

  npm install moat
  var moat = require('moat');
  var context = moat.init();
  var session = context.session;
  var clientRequest = context.clientRequest;
        :
        :

With Sinon.JS and NodeUnit, you can test your scripts like this:

  var nodeUnit = require('nodeunit');
  var sinon = require('sinon');
  var yourScript = require('path/to/your/script.js');

module.exports = nodeUnit.testCase({ setUp: function(callback) { callback(); }, tearDown: function(callback) { callback(); }, 'test your script.' : function(assert) { var sinonContext = moat.initSinon(sinon); var context = sinonContext.getStubContext(); var arguments = { request: JSON.stringify({ uid: "value1", param: "value2" }) }; var dmJob = sinonContext.stubDmJob(arguments); var session = sinonContext.stubMessageSession(); var clientRequest = sinonContext.stubClientRequest(null, null, dmJob);

  context.session = session;
  context.clientRequest = clientRequest;

  session.queryForUid.withArgs('Entity1', 'value1').returns('token');
  session.commit.withArgs('Getting an entity.').returns({
      token: {
          collection: [{
              uid: 'value1',
              field1: 'field1Value'
          }]
      }
  });
  :
  snip
  :
  assert.done();
}

});

NodeUnit provides you 2 ways to run your test code:

  • The nodeunit command line tool
  • API call inside a Node js script

Here is an example test runner code with the latter way.

  var reporter = require('nodeunit').reporters.default;
  reporter.run(['path/to/your/test/code.js']);

Simply you can type 'node test-suite-runner.js' so that you'll see the test results.

Unfortunately, we don't provide any open environment for deployment so far. However, we're planning to launch a small playground on a cloud so that you can try it as well as other MOAT IoT artifacts such MOAT REST and MOAT Android js.

This library is dual-licensed under:

Copyright © 2012 Inventit Inc.

June 29, 2012

  • Initial Release.