Tuesday, 5 May 2015

REST Full API Test case with Mocha in Nodejs

REST Full API Test case with Mocha in Nodejs 

First of all install mocha globally on your node server.

npm install -g mocha

We will also use some more modules to write a test case install that modules also by below steps.

npm install --save supertest
npm install --save assert
npm install --save should

Now create test folder in your project by writing

mkdir test

Now place your testCase.js file in side this folder

Now to run test just write below command

mocha

If you want to run test case using "npm test"  command then create file named makefile without any extension and put below code in that file

test:
 @./node_modules/.bin/mocha

.PHONY: test


Then add the following lines to your package.json file:

"scripts": {
"test": "make test"       //  OR mocha test
}

----------------------------------------------------------------------

testCase.js file
----------------------------------------------------------------------

var assert = require('assert');

var request = require('supertest');
var age = request.agent();
var should = require('should');
var recentevent = "";
var recentuser = "";
var recentcompany = "";

describe('dbop', function () {

    it('should axpect 401 Unauthorized', function (done) {
        request("localhost:3000")
        .get("/users")
        .expect(401) //Status code
        .end(function (err, res) {
            if (err) {
                throw err;
            }

            done();
        });

    });

    function loginUser() {
        return function (done) {
            request("localhost:3000")
            .post('/users')
            .set("Authorization", "basic " + new Buffer("TESTUS:passkey").toString("base64"))
            .expect(200)
            .end(onResponse);

            function onResponse(err, res) {
                if (err)
                    return done(err);
                return done();
            }
        };
    }
    it('should authorized', loginUser());

    function getAllUser() {
        return function (done) {
            request("localhost:3000")
            .get("/users")
            .set("Authorization", "basic " + new Buffer("TESTUS:passkey").toString("base64"))
            .set('accept', 'application/json')
            .expect(200) //Status code
            .end(function (err, res) {
                if (err) {
                    throw new Error("error");
                }
                res.should.have.body;
                console.log(res.body);

                done();
            });

        }
    }

    it('should get all users', getAllUser());

    it('should add user', function (done) {
        request("localhost:3000")
        .post("/users")
        .set("Authorization", "basic " + new Buffer("TESTUS:passkey").toString("base64"))
        .set('accept', 'application/json')
        .send({
            "user_name" : "testUser",
            "passkey" : "testKey",
            "companyid" : "10"
        })
        .expect(200) //Status code
        .end(function (err, res) {
            if (err) {
                throw new Error("error");
            }
            // Should.js fluent syntax applied
            //res.body.should.have.property('id');
            res.should.have.body;
            console.log(res.body);
            recentuser = res.body.id;
            //.should.not.equal(null);
            //res.body.passkey.should.not.equal(null);

            done();
        });

    });

   
});

Result:

restfull-api-test-with-mocha-nodejs


Source code available at https://github.com/jayesh36/RESTFull-API-with-express-knex-promises-with-testcase-in-mocha/tree/master/test