-
Notifications
You must be signed in to change notification settings - Fork 1
Testing Guide
Writing tests are an essential part of any piece of code. Tests can be found in backend/test.
tst.js is used as a scratch pad for testing classes. Use this in development to make sure your classes work as intended. This file is a normal js file can be run explicitly by typing node tst in the backend folder. In this file there is no notion of passing or failing. You can at all times delete any code in tst (leave the imports).
cd backend cd to the backend.
npm run test run the test script.
each step outlined below is explained below the procedure below
- Create a new file
yourtestname.test.js. - Import the request verbs functions (Post, Delete, Get, Put)
- Use one method and pass it the correct parameters (check signatures below)
- All methods have a handler with parameter {expect, res}. The body of this handler is run and contains the tests for that request.
- Within the body of the handler use the
expectfunction to assert facts about the response objectres. - You can expect a status in the response to be 0 (Ok) or expect that status to be 1 (Error), the test passes if the assertion is held true.
const {Post, Delete, Get} = require('../CreateTest')
Get(APIroute:str ,description:str, handler:function)
Post(APIroute:str ,description:str data:json, handler:function)
Delete(APIroute:str ,description:str data:json, handler:function)
Note: The handler function is invoked once the backend returns a response (it's async).
res contains the response json data, expect is a function defined by the CreateTest module. Your actual tests are written within the handler body.
({expect, res})=> { // handler body }
These expect function calls occur with the hander body. These are the assertions made by your test.
Examine the following examples of valid expect calls
eg1. expect(res.body.status).to.equal(1) test passes if status indicates error
eg2. expect(res.body.message).to.equal('Ok') test passes if message is equal to Ok
eg3. expect(true).to.equal(!false) test always passes
Post('/login', 'message should say Welcome', {email: 'ced@gmail.com', password: 'ced'},({expect, res})=>{
expect(res.body.message).to.equal('Welcome') // here the test will pass iff the message is Welcome
})
Nested Requests are possible and valuable for testing through the CRUD process.
- Start by creating the record (Post), the response should contain some sort of id for the record.
- Use that id to get the record (Get)
- Update the record (Put)
- Finally delete that record (Delete).
This will create an idempotent test, allowing it to be run again and again without changing the state of the database.
mockUser = { ... }
Post('/createnewuser','Create User', mockUser,({expect, res})=>{
expect(res.body.status).to.equal(0); // we expect to have no errors, ie status = 0
expect(res.body.message).to.equal('Ok'); // we expect message to be Ok
let insertId = res.body.user_id; // user_id is sent back in the response
mockUser.firstName = "Jon"; // change names
Put('updateuser', 'User should be updated', mockUser, (({expect, res})=>{
expect(res.body.message).to.equal('User Updated.');
expect(res.body.status).to.equal(0);
})
Delete('/deleteuser', 'User should be deleted.', {"user_id":insertId}, ({expect, res})=>{
expect(res.body.message).to.equal('User Deleted.');
expect(res.body.status).to.equal(0);
})
})