Unit Testing Your Application

In the previous lessons, we learned how to use Resources & Resource Controller to simplify the controllers implementation. Now, we need to test our implementation so we can ensure it is handling requests correctly.

Blueprint uses mocha and superagent to facilitate unit testing. The benefit to Blueprint approach is it allows you to run unit tests without needing a front-end, such as Postman, to exercise the Blueprint application. You can also use chai to implement the test oracle for each unit test.

Using our current example, here is a simple unit test to check the GET /api/rentals.

// tests/unit-tests/app/routers/rental.js

const { request } = require ('@onehilltech/blueprint-testing');
const { expect } = require ('chai');

describe ('app | routers | rental', function () {
  context ('GET', function () {
    it ('should get all the rentals', async function () {
      // Send a mock request to the api, and wait for the response.
      const res = await request ()
        .post ('/api/rentals')
        .expect (200);
        
      // res.body has the text from the response. 
      // From here you can use chai to implement test oracle via assertions.
    });
  });
});

Last updated