Git Product home page Git Product logo

botkit-mock's Introduction

Botkit-Mock - Write tests for Botkit.

npm Build Status

Setup

  1. npm install --save botkit-mock
  2. Require botkit-mock in your test: const mock = require('botkit-mock');
  3. Require your controller in your test: const fileBeingTested = require("../controllers/indexController")
  4. Follow test case examples seen here

Basic Usage

Testing Controllers

Let's say you have a controller that looks something like this:

module.exports = function(bot, controller) {
    // simple answer
    controller.hears(['help'], 'direct_message', function (bot, message) {
        bot.reply(message, 'help message');
    });
}

To use botkit-mock, you should setup your controller as follows in your beforeEach:

const mock = require('botkit-mock');
const yourController = require("../yourController");

describe("controller tests",()=>{
    beforeEach((done)=>{
        var self = this;
        self.controller = new mock.controller()
        yourController(self.controller.bot, self.controller)
        done();
    });
});

In your it statement, use the controller.usersInput method to define the conversation.

it('should return `help message` if user types `help`', (done) => {
    var self = this;
    return self.controller.usersInput(
        [
            {
                first: true,
                messages: [
                    {
                        text: 'help', isAssertion: true
                    }
                ]
            }
        ]
    ).then((text) => {
        assert.equal(text, 'help message')
        done()
    })
});

Options

usersInput takes an array of objects with the following fields:

Mandatory

  • first indicates which user spoke first in multi-user testing.
  • messages is an array of objects that have two fields
    • text is the text of the message (Mandatory)
    • isAssertion indicates which conversation response array to return in .then() in multi-user testing.
    • channel indicates the channel the message was sent in

Optional

  • type specify botkit message type. IE direct_message or message_received
  • user specifies the userId of the user sending the message
  • deep indicates the index of the conversation response to return in .then(). 0 is the last response, 1 is the second-to-last, etc..

Testing API

See examples here.

Built by the team at https://www.gratify.chat.

botkit-mock's People

Contributors

amplicity avatar ihorrusinko avatar agamrafaeli avatar osh123 avatar alecl avatar tansaku avatar arreche avatar mtc2013 avatar

Watchers

Luis Aguilar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.