Supplying helpers for test data
Video: Supplying helpers for test dataBefore writing unit tests, the application you're testing must be configured in such a way that you can test all of the end points. It's also helpful to supply some sample data you know won't change. In this video, we'll configure our application and add some sample data. Let's go to the Exercise files > Chapter 6 > Video 3 and then copy the Start folder to the desktop. Next, > Terminal, and change directory to that folder. And now I'm going to type npm install.
Viewers: in countries Watching now:
- Why use Node.js?
- Installing Node.js
- Understanding the event loop
- Initializing Node.js projects
- Creating modules with getters and setters
- Starting Express applications
- Testing your code
- Working with sessions and databases
- Building command-line tools
- Emitting events and attaching listeners
- Controlling readable streams
Supplying helpers for test data
Before writing unit tests, the application you're testing must be configured in such a way that you can test all of the end points. It's also helpful to supply some sample data you know won't change. In this video, we'll configure our application and add some sample data. Let's go to the Exercise files > Chapter 6 > Video 3 and then copy the Start folder to the desktop. Next, > Terminal, and change directory to that folder. And now I'm going to type npm install.
So the first thing I'm going to do, is create a Test folder in my Start folder. This folder is going to contain all of my unit tests as well as all the helper files I'll build to help run them. In the Exercise files, I have some sample data. This is just a set a three flights that I know won't change. Copy this Data folder into the Test folder. Now let's add a Helpers folder. This folder is going to contain an app.js file, which I'm going to use to simulate the application that we already have built.
So let's open this up in our Text Editor. In the Helpers folder, I'm going to create a file, and I'm going to call it app.js. The first thing I'm going to include is the original application So to do that, I'm going to require, I'm going to go down two levels so I can find the app.js file. And now I'm going to include the sample data. I want to include the data in this folder, rather than the data we've been using. So I'm just going to go down one level. And then finally, I want to export this application that's bound with these flights.
So, to do that I'm going to type in module.exports. And then I'm going to set that to the result of calling the application with the flights. Now, let's create a file that will help us test whether or not all this works. We're also going to use this file later on to write actual unit tests. I'm going to create a file in the Test folder, and I'm going to call it lightspeck.js. And all I'm going to do for now on this file, is create a variable called app, and I'm going to set it to requiring helpers app.
So, let's go to the command line and run this file to make sure there are no errors. I'm changing directory to test and now I'm going to type node and flight spec.js. And now I'm going to press Return. No errors came up, so we're good to go. The app helper we've written binds the application to the set of sample data we've added. In the next video we'll install unit testing tools and write some tests.
Find answers to the most frequently asked questions about Node.js Essential Training .
Here are the FAQs that matched your search "" :
- Q: How can I install the new version of the Express command-line tool?
- A: Newer versions of the Express command-line tool can be installed using the command "npm install -g express-generator." But before doing this, you should first remove any older version of the module, using the command "npm uninstall -g express."
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.