Skip to content

Installation

TestMyBot edited this page Feb 9, 2018 · 2 revisions

Usually, you won't install this project on it's own, but you will include it in your Chatbot projects.

To install it to your chatbot project, type:

$ npm install testmybot --save-dev

Please note that you have to install it in your local development directory (not in global registry with -g).

With Jasmine, the setup looks like this:

$ npm install testmybot --save-dev
$ npm install jasmine --save-dev
$ ./node_modules/.bin/jasmine init

Add a file named "testmybot.json" to your project directory. A very basic configuration for a Facebook Chatbot looks like this:

{
  "botium": {
    "Capabilities": {
      "PROJECTNAME": "testmybot-sample1",
      "FACEBOOK_API": true,
      "FACEBOOK_WEBHOOK_PORT": 5000,
      "FACEBOOK_WEBHOOK_PATH": "webhook",
      "CLEANUPTEMPDIR": false,
      "STARTCMD": "node index.js"
    },
    "Envs": {
      "NODE_TLS_REJECT_UNAUTHORIZED": 0,
      "NODE_ENV": "dev"
    }
  }
}

If your Chatbot code is located in a Git repository, you can make Botium load it from there:

{
  "botium": {
    "Capabilities": {
      "PROJECTNAME": "testmybot-sample1",
      "FACEBOOK_API": true,
      "FACEBOOK_WEBHOOK_PORT": 5000,
      "FACEBOOK_WEBHOOK_PATH": "webhook",
      "CLEANUPTEMPDIR": false,
      "STARTCMD": "node index.js"
    },
    "Sources": {
      "GITURL": "https://github.com/jw84/messenger-bot-tutorial",
      "GITPREPARECMD": "npm install"
    },
    "Envs": {
      "NODE_TLS_REJECT_UNAUTHORIZED": 0,
      "NODE_ENV": "dev"
    }
  }
}

You tell TestMyBot that the Facebook Webhook of your chatbot runs on port 5000, and the url path is /webhook.

Add a file spec/testmybot.spec.js with a basic test case:

describe('TestMyBot Sample Conversation Test Suite', function() {
  var bot = require('testmybot');

  beforeAll(function(done) {
    bot.beforeAll().then(done);
  }, 120000);

  beforeEach(function(done) {
    bot.beforeEach().then(done);
  }, 60000);

  afterEach(function(done) {
    bot.afterEach().then(done);
  }, 60000);

  afterAll(function(done) {
    bot.afterAll().then(done);
  }, 60000);

  it('should answer to hello', function(done) {

    bot.hears('hello');

    bot.says().then((msg) => {
      expect(msg.messageText).toMatch(/echo/);
      done();
    }).catch((err) => {
      throw new Error(err);
    });
  });
});

Take special care for:

  • All test are asynchronous
  • Setup and Teardown has high timeouts, because buildling, running and stopping Docker containers can take some time. Especially on first run, it will take very long. Afterwards, the Docker cache speeds up things.
  • The test API is rather simple
    • bot.hears: send a text (or structured content) to your chatbot
    • bot.says: receive a text (or structured content) from your chatbot

You can hand over environment variables to your chatbot here. And finally, run your tests with Jasmine:

$ ./node_modules/.bin/jasmine

In the end, your Jasmine tests should succeed (of course).

Clone this wiki locally