botkit-mock v0.7.5-4
Botkit-Mock - Write tests for Botkit.
Setup
- npm install --save botkit-mock
- Require botkit-mockin your test:const Botmock = require('botkit-mock');
- Require your controller in your test: const fileBeingTested = require("./indexController")
- 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(controller) {
    // simple answer
    controller.hears(['help'], 'direct_message', function (bot, message) {
        bot.reply(message, 'help message');
    });
}To use botkit-mock, you can test your controller like below:
const Botmock = require('botkit-mock');
const yourController = require("./yourController");
describe("controller tests",()=>{
    beforeEach(()=>{
        this.controller = Botmock({});
        // type can be ‘slack’, facebook’, or null
        this.bot = this.controller.spawn({type: 'slack'});
        yourController(this.controller);
    });
});In your it statement, use the bot.usersInput method to define the conversation.
it('should return `help message` if user types `help`', () => {
    return this.bot.usersInput(
        [
            {
                user: 'someUserId',
                channel: 'someChannel',
                messages: [
                    {
                        text: 'help', isAssertion: true
                    }
                ]
            }
        ]
    ).then((message) => {
        // In message, we receive a full object that includes params:
        // {
        //    user: 'someUserId',
        //    channel: 'someChannel',
        //    text: 'help message',
        // }
        return assert.equal(message.text, 'help message');
    })
});Advanced Usage
botExtender - allows developers to extend the bot and add custom functions to the bot. This overrides functionality in BotmockWorker.js.
To use this, define botExtender in your beforeEach. It accepts three parameters (bot, botkit, config), which are passed in from Botmock.js and BotmockWorker.js. Pass botExtender to your .spawn() and you will have access to your custom functions in the it.
beforeEach(()=>{
        function botExtender(bot, botkit, config){
            bot.customReply = function(message, text){
                bot.reply(message, 'Something new...' + text)
            }
        }
        this.bot = this.controller.spawn({type: 'slack', botExtender: botExtender});
    });usersInput options
- useruser slackId (required) (string)
- channelis a channel where user sends messages (required) (string)
- typespecify botkit message type. ie- direct_message,- message_received,- interactive_message_callback. (defaults to- direct_message) (string)
- messages(array) that includes:- isAssertionindicates which conversation response array to return in- .then()in multi-user testing. (required) (boolean)
- deepindicates the index of the conversation response to return in- .then(). 0 (default) is the last response, 1 is the second-to-last, etc.. (integer)
- timeoutset timeout for message in milliseconds (integer)
- waitBeforealias for- timeout, indicates how many milliseconds to wait before sending the message to the bot (integer)
- waitAfterindicates how many milliseconds to wait for the bot response, useful for long-running commands (integer)
- textthe message's text (string)
- channelindicates the channel the message was sent in. This overrides the channel defined in- usersInputfor this current message. (string)
- ...any other fields you may be testing for including attachments,callback_id, etc...
 
Contributing
botkit-mock currently (and will always) support all of Botkit's core functionality by default. We also support extended Slack and Facebook functionality. 
To add functionality to botkit-mock, you can create platform-specific functions like seen in FacebookBotWorker. If you add functionality to support something we don't, please make a PR.
Examples
- botkit-starter-slack - tests for botkit starter kit (files with name *mochaSpec.js)
- convo_bot - tests for simple bot convo  (files with name *MochaSpec.jsor*JasmineSpec.js)
- api - simple api calls and api response overrides
Built by the team at https://www.gratify.chat.
Like botkit-mock? Donate BTC to our team: 1KwpqzTvpLWiUST2V5wmPiT3twwc1pZ9tP
Change Log
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago