Documentation

Do you use documentation to use NewBot Framework

Test a scenario

Type newbot test in the terminal to run the unit tests

Perform a scenario test

Let's initialize the scenario

import { ConverseTesting } from 'newbot/testing'
import mainSkill from './main'

describe('My own test', () => {
    let converse, userConverse

    beforeEach(() => {
        converse = new ConverseTesting(mainSkill)
        userConverse = converse.createUser()
    })
})

The content of main.converse is as follows:

@Event('start') 
start() {
    > Hello
}

The goal is to test if the message sent is Hello when the user interacts for the first time.

So let's add a test:

import assert from 'assert'
import { ConverseTesting } from 'newbot/testing'
import mainSkill from './main'

describe('My own test', () => {
    let converse, userConverse

    beforeEach(() => {
        converse = new ConverseTesting(mainSkill)
        userConverse = converse.createUser()
    })

    it('Chatbot says "Hello"', () => {
        return userConverse
            .start(testing => {
                assert.equal(testing.output(0), 'Hello')
            })
            .end()
    })
})
  1. Let's use the start() method to start the scenario. The function in parameter contains the argument testing. It will be used to perform several tests.
  2. Here, we use the output() function to retrieve the first message sent by the script

Note that the end() method returns a promise. With mocha, we can return the promise to it() function

It is possible to recover all messages sent by the script. For that, do not put parameters to the method.

const array = testing.output() // Tableau de messages

Prompt test

The content of main.converse is now the following:

@Event('start') 
start() {
    > Hello
    Prompt()
    > Welcome { :text }
}

The test :

import assert from 'assert'
import { ConverseTesting } from 'newbot/testing'
import mainSkill from './main'

describe('My own test', () => {
    let converse, userConverse

    beforeEach(() => {
        converse = new ConverseTesting(mainSkill)
        userConverse = converse.createUser()
    })

    it('Test Prompt', () => {
        return userConverse
            .start(testing => {
                assert.equal(testing.output(0), 'Hello')
            })
            .prompt('Sam', testing => {
                assert.equal(testing.output(0), 'Welcome Sam')
            })
            .end()
    })
})

Note that the start() method performs all tests up to the input request. Then we use prompt() to enter a text (first parameter) and the scenario continues.

Perform a quick test of the conversation

It is possible to write a unit test more quickly if you want to test a simple conversation:

The scenario is always the following:

@Event('start') 
start() {
    > Hello
    Prompt()
    > Welcome { :text }
}
import { ConverseTesting, user, bot } from 'newbot/testing'
import mainSkill from './main'

describe('My own test', () => {
    let converse, userConverse

    beforeEach(() => {
        converse = new ConverseTesting(mainSkill)
        userConverse = converse.createUser()
    })

    it('Test conversation', () => {
        return userConverse
            .conversation(
                bot `Hello`,
                user `Sam`,
                bot `Welcome Sam`
            )
    })
})
© 2019 NewBot