Wednesday, 14 June 2017

mocha module with nodeJs tutorial

mocha module with nodeJs tutorial

Question: What is Mocha?
Mocha is a Rich JavaScript framework running on Node.js and in the browser making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.



Question: How to install Mocha?
npm install --global mocha



Question: How to test script with Mocha?
mocha tests.js



Question: How to check Success with Mocha?
Success
function add(a,b){
  return a+b;
}

   
//Success
it('should add two numbers-Success', () => {
  var res = add(33, 11);

  if (res !== 44) {
    throw new Error(`Expected 44, but got ${res}.`)
  }
});

Output
should add two numbers-Success passing (15ms)



Question: How to check Failure with Mocha?
function add(a,b){
  return a+b+2;
}

   
//Failed
it('should add two numbers-Success', () => {
  var res = add(33, 11);

  if (res !== 44) {
    throw new Error(`Expected 44, but got ${res}.`)
  }
});  


Output
1) should add two numbers-Failed 0 passing (25ms) 1 failing 1) should add two numbers-Failed: Error: Expected 44, but got 46. at Context.it (utils\utils.test.js:11:11)


Question: How to write Asynchronous code with Mocha?
describe('User', function() {
  describe('#save()', function() {
    it('should save without error', function(done) {
      var user = new User('Luna');
      user.save(done);
    });
  });
});



Question: How to write Synchronous code with Mocha?
describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      [1,2,3].indexOf(5).should.equal(-1);
      [1,2,3].indexOf(0).should.equal(-1);
    });
  });
});




Question: How we can achieve HOOKS with Mocha?
describe('hooks', function() {
  before(function() { });
  after(function() {  });
  beforeEach(function() {  });
  afterEach(function() {});  
});



Question: What is Github URL for mocha?
https://github.com/mochajs/mocha


Question: In Which language mocha is written?
JavaScript



Question: What is offical website URL of Mocha?
https://mochajs.org/


Monday, 12 June 2017

Node js Redis tutorial - Quick Start

Install redis in nodeJS, Make redis connection and stored data in redis, Get data from redis, update and delete the data from redis.

Question: How to install redis module with node?
npm install redis




Question: How to include redis in nodeJS Project?
var redis = require("redis");



Question: How to connect to redis with nodeJS?
var client = redis.createClient();

//On connection
client.on('connect', function() {
    console.log('connected');
});

//On error of connection
client.on('error', function() {
    console.log('error');
});



Question: How to Store data/Get Data/ Delete data in redis with nodejs?
Store single value
client.set('technology', 'NodeJS');

Get Stored value
client.get('technology');

Delete Stored value
client.del('technology', function(err, reply) {
    console.log(reply);
});



Question: How to check Stored value exist OR Not
client.exists('technology', function(err, reply) {
    if (reply === 1) {
        console.log('Stored in Redis');
    } else {
        console.log('Not Stored in Redis');
    }
});




Question: What is HMSET in nodeJS?
HMSET is like hset but it allow to store value in "key/value" paris.



Question: How to store data in key/value pairs in redis?
client.hmset('frameworks_list', {
    'javascript': 'AngularJS',
    'css': 'Bootstrap',
    'node': 'Express'
}, function(err, msg) {
    if (err) {
        console.log("Error During save the data");
    }
    //console.log(msg);//ok    
});




Question: How to get data which was stored with hmset redis?
client.hgetall('frameworks_list', function(err, msg) {
    if (err) {
        console.log("Error During save the data");
    }
    //console.log(msg);//{javascript: 'AngularJS', css: 'Bootstrap', node: 'Express' }
});



Question: How to store unique values in redis?
client.sadd(['emails', 'abc1@no-spam.ws', 'abc2@no-spam.ws', 'abc3@no-spam.ws'], function(err, msg) {
});



Question: How to get stored values which was set sadd with in redis?
client.smembers('emails', function(err, msg) {
    
});