Get Customer List API – CustomerController – Integration Test – Node.js API with TDD Tutorial

Customer Controller – Get Customer List API Integration Test

Now, let’s continue with the integration test for the Get Customer List functionality. In this section, we will actually test the API endpoint for this functionality which will start with the controller and passes down the request through the middleware and service layers and fetches the data from the MongoDB collection and finally returns the fetched data to the client.

Integration Test 2: Get Customer List API

To run the test for the API endpoint ‘/customers‘ with the GET HTTP method, we need to start writing the test suite for this controller functionality in the customer.controller.js file

Test Suite

 Just below the already written integration test for the create customer API endpoint, add the below describe() block to add the test suite for the current functionality API. As this is for testing the customer module’s base URI with the GET HTTP method, add the describe() block with an appropriate description that defines the test case’s nature.

describe("GET " + baseUri, function () {
});

Inside the test suite, it() block for the test spec will have to be added with the required description of the spec along with the callback ‘done‘ passed as an input parameter which will be invoked once the asynchronous call to the GET API is completed with the response.

Test Script

As you see the completed spec script as below, we have to write the HTTP call to the express app with the base URI with the GET HTTP method call for the integration test to simulate the actual API calling process. Within the end() block of the chai’s HTTP interface, we need to add the expectations for this API’s response. 

As with the add customer API’s integration test, we can check the response status code to be successful with HTTP status code 200, and the data came out of the API call to be an array as we are trying to get the list of customers and it shouldn’t be an empty list though. All the spec’s expectations as described are written as shown below. Finally, to end this asynchronous HTTP call, we have to invoke the callback function done() at the of this test spec, within the end() block.

it('should get all customers', function (done) {
    request(app)
        .get(baseUri)
        .end(function (err, res) {

            expect(res.status).to.equal(200);
            expect(res.body).to.not.equal(undefined);
            expect(res.body).to.be.a('array');
            expect(res.body.length).to.not.equal(0);

            done();
        });
});

Eventually, we will have to see an error for the first failing expectation, status code 200, as we don’t have any functional code in the controller for this API endpoint.

Integration Test 2: CustomerController - Get Customers List API - Failed 1
Integration Test 2: CustomerController – Get Customers List API – Failed 1

Code

To tackle the first failing test for the Get Customer List API endpoint, let’s add the below code in the customer.controller.js file which will add a router definition for the GET HTTP method of the ‘/customers‘ endpoint, and also responds with the empty array once the call is completed.

router.get('/',
    function (req, res) {
        res.status(200).json([]);
    });

It will satisfy the few expectations of the test spec as we are getting an empty array response for the API call. However, still, we haven’t completed the controller’s router definition for getting the customers list from the express application. So we will see the next error as shown below when the test suites ran once again.

Integration Test 2: CustomerController - Get Customers List API - Failed 2
Integration Test 2: CustomerController – Get Customers List API – Failed 2

To resolve the latest error, what we have to do is integrate the middleware method getCustomers() of the CustomerMiddleware in the router definition’s handler methods. So insert the required middleware method just above the existing response handling middleware of the router definition of the ‘/customers’ base URI, just like the completed code shown below.

Also, update the .json() method with the req.response object in the final middleware function as this object is assigned with fetched customers list from through model at the middleware function as you are already aware of.

router.get('/',
    CustomerMiddleware.getCustomers,
    function (req, res) {
        res.status(200).json(req.response);
    });

This code update will completely satisfy all the expectations of the newly written test suite for the get customer list API endpoint and yield all successfully running test scripts as shown below for the integration test cases for CustomerController.

Integration Test 2: CustomerController - Get Customers List API - Passed
Integration Test 2: CustomerController – Get Customers List API – Passed

You can check out the below link to the source code for this step in GitHub.

GitHub – Step 05 – Add Customer API

Please check out the index page for this Node.js RESTful API development with TDD approach tutorial with all the posts in sequence in one place.

This blog post is an excerpt from the book Building Node.js REST API with TDD approach. Please check out the link for more information.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.