DEV Community

Hidetaka Okamoto
Hidetaka Okamoto

Posted on

Testing Alexa Skill by alexa-conversation

via: https://wp-kyoto.net/en/test-by-alexa-conversation

alexa-conversation is a npm packages to testing Alexa Skill.

We can test own skill like following codes.

conversation(opts)
  .userSays('LaunchRequest')
  .plainResponse
  .shouldContain('Welcome to example skills')
  .userSays('AMAZON.StopIntent')
  .plainResponse
  .shouldContain('See you again!')
  .end()

Getting started

If we want to use the package, we have to add mocha at first.

$ npm init -y
$ npm install --save alexa-sdk
$ npm install --save-dev alexa-conversation mocha#

Make Lambda function code

After install these packages, we can make Lambda funciton.
And I made a example code for the test.

index.js

const Alexa = require('alexa-sdk')
const handlers = {
  'LaunchRequest': function () {
    this.emit(':tell', 'Welcome to my skill !')
  },
  'AMAZON.StopIntent': function () {
    this.response.speak('See you again!')
    this.emit(':responseReady')
  }
}

module.exports.hello = (event, context, callback) => {
  alexa = Alexa.handler(event, context, callback)
  alexa.registerHandlers(handlers)
  alexa.execute()
}

Make first test

And let's create first test.

index.test.js

const conversation = require('alexa-conversation')
// Load tested target
const app = require('../index.js')

// init
const opts = {
  name: 'Alexa Sample App', // test name
  appId: 'your-app-id',     // Alexa Skill ID
  app: app,                 // test target
  handler: app.handler      // Lambda handler
}

// write test like conversation
conversation(opts)
  .userSays('LaunchRequest')
  .plainResponse
  .shouldContain('Welcome to my skill')
  .userSays('AMAZON.StopIntent')
  .plainResponse
  .shouldContain('good by!')
  .end()

Running the test

After create these code, we can run own test by mocha cli

$ ./node_modules/mocha/bin/_mocha index.test.js
  Conversation: Alexa Sample App
    User triggers: LaunchRequest 
      ✓ Alexa's plain text response should contain: Welcome to my skill
    User triggers: AMAZON.StopIntent
      1) Alexa's plain text response should contain: good by!
  21 passing (33ms)
  1 failing

  1) Conversation: Alexa Sample App
       User triggers: LaunchRequest 
         Alexa's plain text response should contain: good by!:
     AssertionError: expected ' See you again! ' to include 'good by!'
      at Context.it (node_modules/alexa-conversation/response.js:100:32)

The example test has been failed. Because the AMAZON.StopIntent expects good by! but actual source code returns See you again!.So we have to fix the response to pass the test.

Discussion (1)

Collapse
cubiclebuddha profile image
Cubicle Buddha

Pretty cool. How does this compare to Bespoken’s Virtual Alexa? (I’ve used that and it’s pretty helpful).