Jasmine
Transcript of Jasmine
![Page 1: Jasmine](https://reader035.fdocuments.in/reader035/viewer/2022081514/554f6490b4c905bb178b498a/html5/thumbnails/1.jpg)
Alok Guha
Unit Testing Framework for JavaScript
![Page 2: Jasmine](https://reader035.fdocuments.in/reader035/viewer/2022081514/554f6490b4c905bb178b498a/html5/thumbnails/2.jpg)
![Page 3: Jasmine](https://reader035.fdocuments.in/reader035/viewer/2022081514/554f6490b4c905bb178b498a/html5/thumbnails/3.jpg)
Why Unit Testing ?
• Instant satisfaction• Code Against Your API While or Before it is
Built• Leads to a Better Design• Understand How Your Code Works• Confidence in Your Code
![Page 4: Jasmine](https://reader035.fdocuments.in/reader035/viewer/2022081514/554f6490b4c905bb178b498a/html5/thumbnails/4.jpg)
Why UTs are more important with JavaScript
• Because its weak-typed language• Works on client side.• Faster to test Unit Tests than to browse actual
application.
![Page 5: Jasmine](https://reader035.fdocuments.in/reader035/viewer/2022081514/554f6490b4c905bb178b498a/html5/thumbnails/5.jpg)
JasmineA BDD framework for JavaScript testing
• a behavior-driven development framework for testing JavaScript code.
• does not depend on any other JavaScript frameworks
• does not require DOM.• Can be integrated with any CI tool.
![Page 6: Jasmine](https://reader035.fdocuments.in/reader035/viewer/2022081514/554f6490b4c905bb178b498a/html5/thumbnails/6.jpg)
Suits & Spects
![Page 7: Jasmine](https://reader035.fdocuments.in/reader035/viewer/2022081514/554f6490b4c905bb178b498a/html5/thumbnails/7.jpg)
Expectations & Matchers• To express what you expect about behavior of your
code.• Matcher implements a Boolean comparison between
the actual value and the expected value
![Page 8: Jasmine](https://reader035.fdocuments.in/reader035/viewer/2022081514/554f6490b4c905bb178b498a/html5/thumbnails/8.jpg)
Frequently Used Matchers• The 'toBe' matcher compares with ===• The 'toEqual' matcher• The 'toMatch' matcher is for regular expressions• The 'toBeDefined' matcher compares against `undefined`• The `toBeUndefined` matcher compares against `undefined`• The 'toBeNull' matcher compares against null• The 'toBeTruthy' matcher is for boolean casting testing• The 'toBeFalsy' matcher is for boolean casting testing• The 'toContain' matcher is for finding an item in an Array
Every matcher’s criteria can be inverted by prepending .not
![Page 9: Jasmine](https://reader035.fdocuments.in/reader035/viewer/2022081514/554f6490b4c905bb178b498a/html5/thumbnails/9.jpg)
beforeEach & afterEach• Both takes a function which executes before
and after execution of each spec.
![Page 10: Jasmine](https://reader035.fdocuments.in/reader035/viewer/2022081514/554f6490b4c905bb178b498a/html5/thumbnails/10.jpg)
Spy
• These are mock or fake calls to method.• Spies should be created before expectations.• Spies can be checked if they were called or
not, and what was calling arguments.
![Page 11: Jasmine](https://reader035.fdocuments.in/reader035/viewer/2022081514/554f6490b4c905bb178b498a/html5/thumbnails/11.jpg)
Think Tests ?
![Page 12: Jasmine](https://reader035.fdocuments.in/reader035/viewer/2022081514/554f6490b4c905bb178b498a/html5/thumbnails/12.jpg)
Possible Test cases
• a & b should be defined always.• If a & b defined it should return sum of them.• if a or b is not defined, result should be
undefined.• Result should be in same cast as input
provided.
![Page 13: Jasmine](https://reader035.fdocuments.in/reader035/viewer/2022081514/554f6490b4c905bb178b498a/html5/thumbnails/13.jpg)
Lets welcome Jasmine
• Live examples
![Page 14: Jasmine](https://reader035.fdocuments.in/reader035/viewer/2022081514/554f6490b4c905bb178b498a/html5/thumbnails/14.jpg)
references
• http://pivotal.github.com/jasmine/• http://evanhahn.com/how-do-i-jasmine/• http://try-jasmine.heroku.com/• My own experiences.
![Page 15: Jasmine](https://reader035.fdocuments.in/reader035/viewer/2022081514/554f6490b4c905bb178b498a/html5/thumbnails/15.jpg)