In the last part of this articles series, we have successfully created our Student collection, so now we are ready to perform some operations with it, like creating, reading, or deleting data. But first, I would like to introduce you to the concepts of testing our applications. I will start to do that by pointing to this little quote that I can remember reading in my first programming book some years ago:
A very big part of the developing process will be taken by testing, so developers will often find themselves debugging and writing test cases to make sure that their code is working correctly.
At first I couldn’t believe this, how can I spend so much time testing my code instead of writing it? Why should I spend so much time trying to find every possible error when I could be thinking already about that next super-cool feature I had in my mind?
And then some time and many errors later the answer came:
The importance of testing
We, as developers, have the responsibility to ship to the final user of our application a good quality result, our job is to make life easier for other people, and so we must stress ourselves to make a product usable and efficient. We cannot let errors in our code compromise the time we spent in the developing process. And please note, when I say that we have to ship a quality result, I also refer to:
- Creating quality code that other developers will be able to read and use.
- Creating quality code that we will be able to read and use. Maybe you are not thinking about this, but probably one day you will comeback to some project that you were working on some months ago and wonder what is that mess. And that will be an important realization in how much testing and developing quality stuff is important.
A companion tool
Every important operation has dedicated tools for it they say. And so we have many testing tools that we could use for our application. In particular, as already mentioned earlier, we are going to make use of Mocha. If you have never used it then don’t worry, this article will help you clear your mind. And if you are looking for the complete definition of this tool, then look up on their official website here.
Testing the creation of students
The first test we are going to perform is a simple example test that will be used in the next article as the base for testing the creation of students inside our database.
- First, create a file called: creation-test.js inside of the test folder that we created here.
At this point we can also introduce the first dictionary of terms of this article about the functions Mocha provides us to perform testing:
describe(): At the very top level of our testing files, we will call this function to literally describe the series of test cases that we are about to perform, this function will also show a helpful message inside of the console to express the tests we’re performing.
It(): the blocks where we will insert our code tests.
Writing our tests
Let’s now start to write the tests itself, I will write the complete code here and then I will explain it step by step:
As you can see, both the
it() functions take two parameters, the first one is a String, in the
describe() function this String tells us what tests we are about to perform, in the
it() function this String is used to describe the single test we are performing. The second argument is the body of the function. Inside of the
describe() function body, we will put the
it() functions, and inside of them we will put the code itself for the tests.
Inside of every
it() function that we write we will make use of a mechanism called assertion, to literally assert that a specific value has to be equal to another one, this is the mechanism that we will use inside of our MongoDB applications to test our code.
Mocha doesn’t provide us any assertion tool by default, so we should include the assert function by using the following instruction:
Since testing requires to learn an important load of new stuff all linked together, we are first gonna play around a little with
assert() inside of this article, and then later we will perform the test itself with other useful operations.
Let’s see how this function works in practice by writing a simple example, add this instruction to the scaffolding functions we created earlier:
And that’s it, this is what testing is about using Mocha, we simply use the
assert() function to say that a specific value is equal to something else. Here we are asserting that the value 1 is equal to 1, and as you can guess that is true. To run this test, we will make use of the scripts section inside of the package.json file that we created at the beginning of our project:
As you can see from the content of this file, we have a scripts section that describes useful scripts that we can use together with the npm run command. So here, if we were to run the npm run test command, the result would be:
echo \”Error: no test specified\” && exit 1
And that’s of course not what we want. So substitute that code
Now run the command:
And you should see a message saying something like (don’t pay attention to the deprecation warning message):
And here we are! we now have messages that tell us if our tests went well and this is amazing. From now on we will make heavy use of assertions to test our functionalities. Of course this was just a simple and dumb example, but it was necessary for you to introduce the concepts and the importance of testing. If you want to try to make an assertion fail you would write something like:
And the result would be:
In this article we have learned why testing is important and we have also played around with the basics of Mocha for testing our app. Later, we will perform operations using the Student collection and we will write full, real-world tests to make sure everything is working properly. Stay connected for more articles!