Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
What is Test Driven Development?
According to Wikipedia, it means âRequirements are turned into very specific test cases, then the software is improved to pass the new tests, only.â Basically, you would first have to write the test of the software/feature you are developing before you start working on the developing the software/feature itself.
Here are a few more articles on the benefits and why you should apply TDD:- Test Driven Development by Jan Olbrich- Isnât TDD twice the work? Why should you care? by Navdeep Singh
Preparation
Here are a few thing you might want to have set up or installed before getting started:
- Amazon Web Service account (a free one will work just fine)
- node.js (personally though, I prefer installing and maintaining it through nvm)
- Text editor of your choice
Setting up
- Create a new project folder (mkdir project-name)
- Navigate into that new folder (cd project-name)
- Initialize npm (npm init)
- Follow through everything as you prefer (if you donât know what it is, just press enter and itâll keep the default) except for test command:, put in node test/testflow.js testDialog.txt instead. The command line should look something like the snippet below.
This utility will walk you through creating a package.json file.It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fieldsand exactly what they do.
Use `npm install <pkg>` afterwards to install a package andsave it as a dependency in the package.json file.
Press ^C at any time to quit.package name: (emptyproject) version: (1.0.0) description: entry point: (index.js) test command: node test/testflow.jsgit repository: keywords: author: license: (ISC)
Now you have node.js setup for the project.
Unit testing
Shout out to robmccauley for maintaining testflow that provides unit testing for Alexa skills. Hereâs what the following commands will do :
- Clone testflow repository into your project at the âtestâ folder. (If this project is also a git project itself, I recommend doing git submodule add instead of git clone.)
- Create a folder named âdialogsâ
- Create a file named âdefault.txtâ in the âdialogâ folder
- Edit the âSourceCodeFileâ line in the cloned âtestflowâ repository to point to the âindex.jsâ in the base level of the folder (feel free to edit it to your personal preference).
git clone git@github.com:robm26/testflow.git testmkdir dialogstouch dialogs/default.txtsed -i '9c const SourceCodeFile = "../index.js"' test/testflow.js
Inside the âdefault.txtâ file in the âdialogsâ folder, you should have the intents you are trying to test. The default in the testflow repository consist of the followings but you should definitely add some more customized ones to it depending on what you are trying to do in your skill.
LaunchRequestAMAZON.HelpIntentAMAZON.StopIntent
In case where you are taking in variables through utterances, you can do something like this :
DoSomething Input1=userInput1 Input2=userInput2
To get a deeper understanding into using testflow, I recommend looking into its tutorial page and the blog post by the author himself.
Developing the skill
To be honest, I donât have too much to talk about on this topic. Here is some sample code of how a sample skill can look like. (Also remember to do npm install --save alexa-sdk.)
I recommend looking into the official Alexa GitHub repository that provides all kinds of sample skills that you can work on top of.
Deployment
Alexa Skill Kit
Go to this page and select âCreate Skillâ. In there you should see options to âadd intentsâ and âcustom slot typesâ. You should add the appropriate slot types and intents accordingly from above. After you are done with that, select âEndpointâ from the side bar and select âAWS Lambda ARNâ as the skillâs service endpoint. Make a copy of the Skill ID as you will need it later.
AWS Lambda
Create an AWS Lambda function selecting âfrom scratchâ. After that, you should have an option to âadd triggerâ where you would select âAlexa Skills Kitâ. Now in the configuration dialog, you should put in the Skill ID you took a copy of previously then select âAddâ.
After this, select back to the box that labeled your project name. There should be a âFunction codeâ dialog box appeared below. While you can definitely copy and paste your code onto the online text editor, there might be chance where the packages you have installed and used in your code is included by default. It is also possible that you have more than 1 JavaScript files for the skill.
In this case, you would select âUpload a .ZIP fileâ under âCode entry typeâ. Locally, zip up all the JavaScript files along with the node_modules folder then upload the resulting zip file.
Submission
When you are done, you can go back to the Alexa Skill Kit page, select the âDistributionâ tab and fill out the appropriate information before submitting your skill for review. Hopefully everything goes well and your skill gets published at the Alexa Skill Store!
About me
I spend a lot of my free time experimenting and building new things with technologies I find fun and interesting. Follow my exploration journey here or on GitHub.
Credits
- Build Skills with the Alexa Skills Kit
- TestFlow: Simulate Conversations with Your Alexa Skill Code to Ease Debugging
Test Driven Development with Alexa SDK was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.