- In my previous post we have learnt about MochaJS framework.We will learn ShouldJS in this post.
- ShouldJS is a behavioral driven test framework.
- ShouldJS is an expressive framework and makes the test cases simple and keeps it cleaner.
- In this Demo,”We will learn to use MochaJS with ShouldJS for testing Asynchronous calls”.
- The ShouldJS can be installed using npm install should –save-dev command.The following screenshot shows ShouldJS installation.
- Mocha installation can be installed using npm install Mocha –save-dev command.The following screenshot shows the of MochaJS installation .
- For demonstration of ShouldJS we have created ShouldJSDemo directory.The following screenshot shows the structure of ShouldJSDemo directory.
- After installation the package.json has the entries for Mocha and Should.An entry to script property is added to run Mocha test framework.
{
"name": "should-test-demo",
"version": "1.0.0",
"scripts": {
"test": "mocha"
},
"devDependencies": {
"mocha": "^2.3.2",
"should": "^7.1.0"
}
}
- The number-operation.js file contains the code for adding two number and export the object contains the addTwoNumber() method.
var operation ={
addTwoNumber : function(number1, number2){
return number1 + number2;
}
};
module.exports = operation;
- The test/test.js file contains the code testing the addTwoNumber() method.It has used should.be.exactly() method to test addition of 2 numbers.
var assert = require("assert"),
should = require("should")
operation = require('../number-operation');
describe('Testing addTwoNumber', function() {
it('should return 4 when the input number are 1 and 3', function () {
operation.addTwoNumber(1,3).should.be.exactly(4);
});
});
- Now we can run the test case using npm run test command.The following screenshot shows the terminal with test case executed and passed.
- The demo code can be downloaded from the following link:-
https://github.com/saan1984/ShouldJSDemo
