Saturday, November 07, 2015

Custom Matchers in Jasmine 2.3

Custom matchers, provided by the behavior-driven development framework Jasmine, is a great feature for testing JavaScript code. Custom Matchers helps to group multiple catch checking which makes the test cases clean and self explanatory.

Let’s look at an example that I tried recently

Here is a Calculator.js file:

   1: var Calculator = function () { };
   2:  
   3: Calculator.prototype.add = function (a, b) {
   4:     return a + b;
   5: };
   6:  
   7: Calculator.prototype.divide = function (a, b) {
   8:     return a / b;
   9: };

One of the test scenarios that I would like to try is to check whether the result of the method is in a particular range. The test method will look like this with existing matchers.



   1: describe('Calculator', function () {
   2:     var calc;
   3:  
   4:     beforeEach(function () {
   5:         calc = new Calculator();       
   6:     });
   7:  
   8:     it('should be able to add 1 and 1', function () {
   9:         expect(calc.add(1, 1)).toBe(2);
  10:     });
  11:  
  12:     it('should be able to divide 6 by 2', function () {
  13:         expect(calc.divide(6, 2)).toBe(3);
  14:     })
  15:  
  16:     it('should be able to divide a rational number', function () {
  17:         expect(calc.divide(1, 3)).toBeLessThan(0.34);
  18:         expect(calc.divide(1, 3)).toBeGreaterThan(0.3);        
  19:     })
  20: });

But you can replace the existing matchers with a custom matcher like this.



   1: describe('Calculator', function () {
   2:     var calc;
   3:  
   4:     beforeEach(function () {
   5:         calc = new Calculator();
   6:  
   7:         jasmine.addMatchers({
   8:             toBeBetween: function (util, customEqualityTesters) {
   9:                 return {
  10:                     compare: function (actual, a, b) {
  11:                         var result = {};
  12:                         result.pass = actual >= a && actual <= b;
  13:                         return result;
  14:                     }
  15:                 }
  16:             }
  17:         });
  18:     });
  19:  
  20:     it('should be able to add 1 and 1', function () {
  21:         expect(calc.add(1, 1)).toBe(2);
  22:     });
  23:  
  24:     it('should be able to divide 6 by 2', function () {
  25:         expect(calc.divide(6, 2)).toBe(3);
  26:     })
  27:  
  28:     it('should be able to divide a rational number', function () {
  29:         expect(calc.divide(1, 3)).toBeBetween(0.3, 0.34);
  30:     })
  31: });

You can reuse custom matchers to make the test cases clear and more simple.


What are some scenarios that you use custom matchers?

Monday, May 11, 2015

JavaScript Data Binding Frameworks

Data binding is the process that establishes a connection between the application UI and business logic. It synchronize the elements of the application with different sources of data.
Over the last few years several JavaScript libraries have been released to handle the process of binding data to HTML controls which includes:
  • AngularJS
  • Backbone.js
  • Derby
  • Ember
  • JsViews
  • JQXB Expression Binder
  • Knock
  • Meteor
  • Simpli5
  • WinJS

Thanks,
Bimal


Thursday, January 15, 2015

Basic git operations

Git is a version control system and GitHub is a web-page on which we can publish our Git repositories and collaborate with other people.

The basic commands used are:

To initialize a project in local, go to the project directory and use the following command which will create a .git folder.

   1: >git init <project name>

To check the status of the repository use the status command.


   1: >git status

To track the files created or modified locally, add command should be used.


   1: >git add *

or



   1: >git add *.js


or



   1: >git add <file name>

To attach git repo to local workspace


   1: >git remote add origin <git url>
To commit changes to head


   1: >git commit –m “Commit my changes”
To push the changes to master branch in server


   1: >git push -u origin master
To pull an existing repo to the local


   1: >git pull


Thanks.
Bimal

Friday, January 09, 2015

RESTful Framework for Java

  • Apache CXF
  • Apache Tuscany
  • Apache Wink
  • Cuubez framework
  • Jersey
  • RESTeasy
  • Restfulie
  • Restlet