Testing the API
To make sure that everything works, we will write a few tests covering all the methods mentioned in the previous sections. In Chapter 9, Automate Your Testing with Node.js, we learned about Jasmine and Mocha test frameworks. The following test suite uses Jasmine. We will also need one additional module to make HTTP requests. The module is called request
and we can get it using npm install request
or by adding it to our package.json
file. The following are the steps along with the code to test the API:
Let's first test the creation of a new database record:
var request = require('request'); var endpoint = 'http://127.0.0.1:9000/'; var bookID = ''; describe("Testing API", function() { it("should create a new book record", function(done) { request.post({ url: endpoint + '/book', form: { name: 'Test Book', author: 'Test Author' } }, function (e, r, body) { expect(body).toBeDefined(); expect(JSON.parse(body).message).toBeDefined...