📃
NewBot
  • NewBot, what is it?
  • ConverseScript Syntax
    • Variables
    • Arithmetic
    • Boolean
    • Array and object
    • Entering text and output
    • Magic variables
    • Condition
    • Loop
    • Functions
    • The decorators
      • Use @Condition
      • Use @Action
      • Use @Format
      • Use @Intent
      • Use @Event
  • Prebuilt Formats for Widget
    • Quick Replies
    • Multi Cards
    • Articles
    • Form
  • Pre-built function for the widget
    • Request()
    • Map()
    • _ (lodash)
  • Get Started with Framework
    • Install
    • Main Skill
    • Write the conversational script
  • Use NewBot Framework JS
    • Create a skill (more details)
      • Relationship between skills
      • Use the functions of a child competency
      • Conditional
      • Control access to a skill
    • Create a JavaScript function and trigger it in ConverseScript
    • The constants
    • Write formats
      • Write and use multi-formats
      • Share formats
    • Internationalization (i18n)
    • Set up an NLP system
      • Share the NLP system
      • Use DialogFlow
    • Deploy on the browser
    • Run the chatbot on NodeJS
    • Send data when running the chatbot
    • The middlewares
    • Save the user's progress
    • User object
  • Unit Tests
    • Test a scenario
    • Test variables
    • Spy a function
    • Create a mock
    • Test the events
    • Test the actions
Powered by GitBook
On this page
  • Perform a scenario test
  • Prompt test
  • Perform a quick test of the conversation

Was this helpful?

  1. Unit Tests

Test a scenario

Type npm 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()
    })

    test('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()
    })

    test('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()
    })

    test('Test conversation', () => {
        return userConverse
            .conversation(
                bot `Hello`,
                user `Sam`,
                bot `Welcome Sam`
            )
    })
})
PreviousUser objectNextTest variables

Last updated 3 years ago

Was this helpful?