Created
June 23, 2018 12:36
-
-
Save hideokamoto/097a6e7024d1725ae35f0a64b9dabccb to your computer and use it in GitHub Desktop.
ask-sdk-coreを使った実装のユニットテストコードサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Alexa = require('ask-sdk-core'); | |
const { BaseSkillFactory } = Alexa; | |
const assert = require('power-assert') | |
const { handler , HelpIntentHandler } = require('../../index.js') | |
const event = { | |
session: { | |
new: true, | |
sessionId: 'amzn1.echo-api.session.[unique-value-here]', | |
attributes: {}, | |
user: { | |
userId: 'amzn1.ask.account.[unique-value-here]' | |
}, | |
application: { | |
applicationId: 'amzn1.ask.skill.[unique-value-here]' | |
} | |
}, | |
version: '1.0', | |
request: { | |
locale: 'en-US', | |
timestamp: '2016-10-27T18:21:44Z', | |
type: 'AMAZON.Help', | |
requestId: 'amzn1.echo-api.request.[unique-value-here]' | |
}, | |
context: { | |
AudioPlayer: { | |
playerActivity: 'IDLE' | |
}, | |
System: { | |
device: { | |
supportedInterfaces: { | |
AudioPlayer: {} | |
} | |
}, | |
application: { | |
applicationId: 'amzn1.ask.skill.[unique-value-here]' | |
}, | |
user: { | |
userId: 'amzn1.ask.account.[unique-value-here]' | |
} | |
} | |
} | |
} | |
describe('test', () => { | |
it('Lambdaをinvokeしてテストした場合', (done) => { | |
handler(event, {}, (error, result) => { | |
assert.equal(error, null) | |
assert.equal(result.response.outputSpeech.ssml, "<speak>Sorry, I can\'t understand the command. Please say again.</speak>"); | |
done() | |
}) | |
}) | |
it('IntentHandler単体のテスト', async() => { | |
const { DefaultHandlerAdapter, ResponseFactory } = Alexa; | |
const handlerAdapter = new DefaultHandlerAdapter() | |
const Input = { | |
responseBuilder: ResponseFactory.init(), | |
requestEnvelope: event | |
} | |
const response = await handlerAdapter.execute(Input, HelpIntentHandler) | |
assert.equal(response.outputSpeech.ssml, "<speak>You can say hello to me!</speak>") | |
assert.equal(response.shouldEndSession, false) | |
}) | |
it('responseBuilderを使ってない場合は、こんな感じでもいける', async() => { | |
const { DefaultHandlerAdapter } = Alexa; | |
const handlerAdapter = new DefaultHandlerAdapter() | |
const testHandler = { | |
canHandle: function(){ return true}, | |
handle: function (handlerInput) { | |
return {shouldEndSession: true} | |
} | |
} | |
const response = await handlerAdapter.execute(null, testHandler) | |
assert.deepEqual(response, {shouldEndSession: true}) | |
}) | |
it('canHandleのテストだけやる場合', () => { | |
const param = { | |
requestEnvelope: event | |
} | |
const result = HelpIntentHandler.canHandle(param) | |
assert.equal(result, false) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment