UNIT TESTING USING JASMINE JAVASCRIPT
IMPORTANCE OF UNIT TEST  Makes the process Agile  Improves the Quality of Code  Finds application issues/bugs early  Detect Changes that may break the design contract in refactoring  Provide debugging process  Provides documentation  Reduces cost
AVAILABLE UNIT TEST FRAMEWORKS Find more unit test frameworks - https://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#JavaScript
WHY JASMINE FRAMEWORK  Behaviour driven development framework, also supports TDD  Used for synchronous and asynchronous javascript code  Does not rely on DOM or any browser  Has built in assertion library and command line utility to run the tests  Has simple syntax 
HELLO WORLD EXAMPLE Javascript function – - return ‘Hello World’ String Unit test case for Javascript function – - Calls actual function and matches the output with expected output
INSTALL JASMINE FRAMEWORK  Download latest version of Jasmine framework – click here  Extract all the files  Open ‘SpecRunner.html’ file
JASMINE FILE STRUCTURE Boot Js – this file "boots" Jasmine, performing all of the necessary initialization before executing the loaded environment and all of a project's specs. Spec folder - Contains the Javascript tesintg files unit test files Src folder - Contains the Javascript source files Jasmine-html.js – Javascript library which has pure js functions to capture the running statistics of suits / specs Jasmine.js – Jasmine’s core engine which responsible to manage test life cycle SpecRunner.html– Test case runner HTML file
CREATE TEST AND TEST SUITE  describe(string suiteName) - Declare test suites using this function  Group of all respective it() /test cases  it (string testName) – Declare test case  Write multiple it() calls in a single describe() function  The string you passed to the describe() as the first parameter will be concatenated with the string passed to the it(), to describe the complete name of the spec. Syntax : describe(‘Suite Name – Scenario name’, function() { it(‘test case 1 name’, function() { ... }; it(‘test case 2 name’, function() { ... }; })
MATCHERS • Any matcher can evaluate to a negative assertion by chaining the call to expect with a not before calling the matcher • Each matcher implements a boolean comparison between the actual value and the expected value. • It is responsible for reporting to Jasmine if the expectation is true or false. Jasmine will then pass or fail the spec.
LIST OF MATCHERS
SETUP AND TEARDOWN METHODS  beforeEach() and afterEach() – runs before and after for every suite  beforeAll() and afterAll() – runs before and after all the suites before All afterAll beforeEach Test afterEach Test Suits
TEARDOWN METHODS EXAMPLE beforeEach and afterEach Example beforeAll and afterAll Example
EXAMPLE - MATH.JS • Create new file in SRC Folder • Set file name as ‘Math.js’ • Create these functions for addition, subtraction • Make an entry in specRunner.html file
EXAMPLE - MATH-SPEC.JS • Create new file in ‘spec’ Folder • Set file name as ‘Math-spec.js’ • Make an entry in SpecRunner.html file
JASMINE SPEC-RUNNER DASHBOARD • Open SpecRunner.html file in any browser • Calculator is the name of the ‘Test suite’ • Name start with ‘should’ are the spec /scenario description
FAILING SPEC  The fail () function causes a spec to fail.  It can take a failure message or an Error object as a parameter.
FAILING SPEC RESULTS
THANK YOU

Unit testing using jasmine in Javascript

  • 1.
    UNIT TESTING USINGJASMINE JAVASCRIPT
  • 2.
    IMPORTANCE OF UNITTEST  Makes the process Agile  Improves the Quality of Code  Finds application issues/bugs early  Detect Changes that may break the design contract in refactoring  Provide debugging process  Provides documentation  Reduces cost
  • 3.
    AVAILABLE UNIT TESTFRAMEWORKS Find more unit test frameworks - https://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#JavaScript
  • 4.
    WHY JASMINE FRAMEWORK Behaviour driven development framework, also supports TDD  Used for synchronous and asynchronous javascript code  Does not rely on DOM or any browser  Has built in assertion library and command line utility to run the tests  Has simple syntax 
  • 5.
    HELLO WORLD EXAMPLE Javascriptfunction – - return ‘Hello World’ String Unit test case for Javascript function – - Calls actual function and matches the output with expected output
  • 6.
    INSTALL JASMINE FRAMEWORK Download latest version of Jasmine framework – click here  Extract all the files  Open ‘SpecRunner.html’ file
  • 7.
    JASMINE FILE STRUCTURE BootJs – this file "boots" Jasmine, performing all of the necessary initialization before executing the loaded environment and all of a project's specs. Spec folder - Contains the Javascript tesintg files unit test files Src folder - Contains the Javascript source files Jasmine-html.js – Javascript library which has pure js functions to capture the running statistics of suits / specs Jasmine.js – Jasmine’s core engine which responsible to manage test life cycle SpecRunner.html– Test case runner HTML file
  • 8.
    CREATE TEST ANDTEST SUITE  describe(string suiteName) - Declare test suites using this function  Group of all respective it() /test cases  it (string testName) – Declare test case  Write multiple it() calls in a single describe() function  The string you passed to the describe() as the first parameter will be concatenated with the string passed to the it(), to describe the complete name of the spec. Syntax : describe(‘Suite Name – Scenario name’, function() { it(‘test case 1 name’, function() { ... }; it(‘test case 2 name’, function() { ... }; })
  • 9.
    MATCHERS • Any matchercan evaluate to a negative assertion by chaining the call to expect with a not before calling the matcher • Each matcher implements a boolean comparison between the actual value and the expected value. • It is responsible for reporting to Jasmine if the expectation is true or false. Jasmine will then pass or fail the spec.
  • 10.
  • 11.
    SETUP AND TEARDOWNMETHODS  beforeEach() and afterEach() – runs before and after for every suite  beforeAll() and afterAll() – runs before and after all the suites before All afterAll beforeEach Test afterEach Test Suits
  • 12.
    TEARDOWN METHODS EXAMPLE beforeEachand afterEach Example beforeAll and afterAll Example
  • 13.
    EXAMPLE - MATH.JS •Create new file in SRC Folder • Set file name as ‘Math.js’ • Create these functions for addition, subtraction • Make an entry in specRunner.html file
  • 14.
    EXAMPLE - MATH-SPEC.JS •Create new file in ‘spec’ Folder • Set file name as ‘Math-spec.js’ • Make an entry in SpecRunner.html file
  • 15.
    JASMINE SPEC-RUNNER DASHBOARD •Open SpecRunner.html file in any browser • Calculator is the name of the ‘Test suite’ • Name start with ‘should’ are the spec /scenario description
  • 16.
    FAILING SPEC  Thefail () function causes a spec to fail.  It can take a failure message or an Error object as a parameter.
  • 17.
  • 18.