Create a mock

The purpose of the mock is to distort the return of a function. This can be useful, for example for an HTTP request

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)
        converse.mock('Request', (url) => {
            return {
                statusCode: 200
            }
        })
        userConverse = converse.createUser()
    })

    test('Test fonction return', () => {
        return userConverse
            .start(testing => {
                const { statusCode } = testing.magicVariable('response')
                assert.equal(statusCode, 200)
            })
            .end()
    })
})

And the ConverseScript code:

@Event('start')
start() {
    Request('https://jsonplaceholder.typicode.com/users')
}

So we use the mock() method to mock a function in the ConverseScript

Last updated