npm install --save botkit-mock
- Require
botkit-mock
in your test:const mock = require('botkit-mock');
- Require your controller in your test:
const fileBeingTested = require("../controllers/indexController")
- Follow test case examples seen here
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()
})
});
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 fieldstext
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. IEdirect_message
ormessage_received
user
specifies the userId of the user sending the messagedeep
indicates the index of the conversation response to return in.then()
. 0 is the last response, 1 is the second-to-last, etc..
See examples here.
Built by the team at https://www.gratify.chat.