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

Monday, December 29, 2014

Basic Angular Application–Step by Step




Angular JS is a JavaScript framework from Google which helps to build structured, testable Single Page Applications(SPA).
Application Requirement
  1. Add a <script> tag which point to angular.js
  2. Add an ng-app attribute(it is a directive  and ng is short form of angular)
Step by Step
To build a basic Angular JS application, follow the steps shown below
  1. Create a html file or a project with a html file in your favorite IDE. I am using WebStorm 9.0.2 trial version and named the project as BasicAngular as shown below.

    image
  2. Add angular.js framework Angular JS framework can be pointing to a local copy of the angular.js file or from a CDN (content delivery network). The screenshot below shows the Google CDN URL.
    image
  3. Add ng-app attribute Include ng-app directive to auto bootstrap the angular application.
    image
  4. Run or open in a browser The expression 12 * 34 is included inside {{ }} will be evaluated and resulting html will be inserted into the div tag as shown below
    image
Thanks,
Bimal

Wednesday, October 22, 2014

Java Frameworks for RESTful Web Services



API Provider
Cuubez

CXF Apache
Dropwizard Yammer
Jersey Oracle
RESTEasy JBoss
Restlet

Tuscany Apache
Wink Apache
Restfulie Caelum

Tuesday, August 26, 2014

Java 7 Programming Language features

Java 7 programming language includes a few new language features which includes
  • Binary Literals
  • Strings in Switch Statements
  • Automatic resource management with try-with-resources Statement
  • Catching Multiple Exception Types and Re-throwing Exceptions with Improved Type Checking
  • Underscores in Numeric Literals
  • Type Inference for Generic Instance Creation or Diamond Operator