Type npm test
in the terminal to run the unit tests
Perform a scenario test
Let's initialize the scenario
Copy 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:
Copy @ 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:
Copy 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 ()
})
})
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.
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.
Copy const array = testing .output () // Tableau de messages
Prompt test
The content of main.converse
is now the following:
Copy @ Event ( 'start' )
start () {
> Hello
Prompt ()
> Welcome { :text }
}
The test :
Copy 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:
Copy @ Event ( 'start' )
start () {
> Hello
Prompt ()
> Welcome { :text }
}
Copy 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`
)
})
})