First Web Application made easy with NodeJS Part 3
From our last part, we wrote code without test, it’s UNACCEPTABLE!!
Prepare to test!
In this part, we’re going to test our API using Chakram.
If you never heard of Mocha or don’t have it, you have to install it too.
- Create a directory tests and create a sub-directory called api
And make a courses.js in that sub-directory.
2. Put the following code
Lines 6 : we give a description about our tests.
Lines 8 : we can initialize some data that we need before all tests start.
Lines 11 : we give a brief description about our test case.
Lines 12–17 : we use Chakram to make a HTTP GET request to our site and we expect that the actual result will match the Lines 15–17.
Line 22 : if we want something to run after all test are done. we can write it here.
3. Run it!
4. Add more tests
I don’t think I need to explain about this file because this file is easy to read, someone may think that it is the same as English language (This is the POWER of BDD style!!)