SlideShare uma empresa Scribd logo
1 de 15
Unit Testing Framework for JavaScript




              Alok Guha
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
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.
Jasmine
      A 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.
Suits & Spects
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
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
beforeEach & afterEach
• Both takes a function which executes before
  and after execution of each spec.
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.
Think Tests ?
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.
Lets welcome Jasmine
• Live examples
references
•   http://pivotal.github.com/jasmine/
•   http://evanhahn.com/how-do-i-jasmine/
•   http://try-jasmine.heroku.com/
•   My own experiences.
Jasmine

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Being Lean Agile
Being Lean AgileBeing Lean Agile
Being Lean Agile
 
Inside Behavior Driven Development
Inside Behavior Driven DevelopmentInside Behavior Driven Development
Inside Behavior Driven Development
 
Karate - powerful and simple framework for REST API automation testing
Karate - powerful and simple framework for REST API automation testingKarate - powerful and simple framework for REST API automation testing
Karate - powerful and simple framework for REST API automation testing
 
The Art of Giving and Receiving Code Reviews
The Art of Giving and Receiving Code ReviewsThe Art of Giving and Receiving Code Reviews
The Art of Giving and Receiving Code Reviews
 
Bdd – with cucumber and gherkin
Bdd – with cucumber and gherkinBdd – with cucumber and gherkin
Bdd – with cucumber and gherkin
 
Few minutes To better Code - Refactoring
Few minutes To better Code - RefactoringFew minutes To better Code - Refactoring
Few minutes To better Code - Refactoring
 
Karate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made SimpleKarate - Web-Service API Testing Made Simple
Karate - Web-Service API Testing Made Simple
 
Shift Remote: Mobile - Efficiently Building Native Frameworks for Multiple Pl...
Shift Remote: Mobile - Efficiently Building Native Frameworks for Multiple Pl...Shift Remote: Mobile - Efficiently Building Native Frameworks for Multiple Pl...
Shift Remote: Mobile - Efficiently Building Native Frameworks for Multiple Pl...
 
Improve your development skills with Test Driven Development
Improve your development skills with Test Driven DevelopmentImprove your development skills with Test Driven Development
Improve your development skills with Test Driven Development
 
Refactoring
RefactoringRefactoring
Refactoring
 
Tdd for php
Tdd for phpTdd for php
Tdd for php
 
5 levels of api test automation
5 levels of api test automation5 levels of api test automation
5 levels of api test automation
 
Growing Manual Testers into Automators
Growing Manual Testers into AutomatorsGrowing Manual Testers into Automators
Growing Manual Testers into Automators
 
Introduction to Bdd and cucumber
Introduction to Bdd and cucumberIntroduction to Bdd and cucumber
Introduction to Bdd and cucumber
 
Insprint automation, build the culture
Insprint automation, build the cultureInsprint automation, build the culture
Insprint automation, build the culture
 
Designing REST APIs
Designing REST APIsDesigning REST APIs
Designing REST APIs
 
RSpec: What, How and Why
RSpec: What, How and WhyRSpec: What, How and Why
RSpec: What, How and Why
 
Jumpstarting Testing In Your Organization with Selenium, Cucumber, & WebdriverIO
Jumpstarting Testing In Your Organization with Selenium, Cucumber, & WebdriverIOJumpstarting Testing In Your Organization with Selenium, Cucumber, & WebdriverIO
Jumpstarting Testing In Your Organization with Selenium, Cucumber, & WebdriverIO
 
Styled Components & React.js
Styled Components & React.jsStyled Components & React.js
Styled Components & React.js
 
API Testing: Answers to Your Top 3 Questions
API Testing: Answers to Your Top 3 QuestionsAPI Testing: Answers to Your Top 3 Questions
API Testing: Answers to Your Top 3 Questions
 

Destaque (7)

Java Script Promise
Java Script PromiseJava Script Promise
Java Script Promise
 
Design patterns
Design patternsDesign patterns
Design patterns
 
NodeJS
NodeJSNodeJS
NodeJS
 
Portfolio Undefined
Portfolio UndefinedPortfolio Undefined
Portfolio Undefined
 
Express JS
Express JSExpress JS
Express JS
 
Q unit
Q unitQ unit
Q unit
 
Aglie estimation and planning
Aglie estimation and planningAglie estimation and planning
Aglie estimation and planning
 

Semelhante a Jasmine

Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
TO THE NEW | Technology
 

Semelhante a Jasmine (20)

Developer Testing
Developer TestingDeveloper Testing
Developer Testing
 
Angular Unit Testing
Angular Unit TestingAngular Unit Testing
Angular Unit Testing
 
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Unit testing of java script and angularjs application using Karma Jasmine Fra...Unit testing of java script and angularjs application using Karma Jasmine Fra...
Unit testing of java script and angularjs application using Karma Jasmine Fra...
 
Testing Angular
Testing AngularTesting Angular
Testing Angular
 
Unit Testing Your Application
Unit Testing Your ApplicationUnit Testing Your Application
Unit Testing Your Application
 
Learn To Test Like A Grumpy Programmer - 3 hour workshop
Learn To Test Like A Grumpy Programmer - 3 hour workshopLearn To Test Like A Grumpy Programmer - 3 hour workshop
Learn To Test Like A Grumpy Programmer - 3 hour workshop
 
My Little Webap - DevOpsSec is Magic
My Little Webap - DevOpsSec is MagicMy Little Webap - DevOpsSec is Magic
My Little Webap - DevOpsSec is Magic
 
Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvu
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - Kenya
 
Finding a good development partner
Finding a good development partnerFinding a good development partner
Finding a good development partner
 
Tackling Testing Telephony
Tackling Testing Telephony Tackling Testing Telephony
Tackling Testing Telephony
 
Test Driven Development and Automation
Test Driven Development and AutomationTest Driven Development and Automation
Test Driven Development and Automation
 
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
 
Ch11lect1 ud
Ch11lect1 udCh11lect1 ud
Ch11lect1 ud
 
Why Automated Testing Matters To DevOps
Why Automated Testing Matters To DevOpsWhy Automated Testing Matters To DevOps
Why Automated Testing Matters To DevOps
 
Getting Ahead of Delivery Issues with Deep SDLC Analysis by Donald Belcham
Getting Ahead of Delivery Issues with Deep SDLC Analysis by Donald BelchamGetting Ahead of Delivery Issues with Deep SDLC Analysis by Donald Belcham
Getting Ahead of Delivery Issues with Deep SDLC Analysis by Donald Belcham
 
Tdd using Jasmine and karma
Tdd using  Jasmine and karmaTdd using  Jasmine and karma
Tdd using Jasmine and karma
 
The Many Ways to Test Your React App
The Many Ways to Test Your React AppThe Many Ways to Test Your React App
The Many Ways to Test Your React App
 
Coding Together - A Dev Workflow
Coding Together - A Dev WorkflowCoding Together - A Dev Workflow
Coding Together - A Dev Workflow
 

Jasmine

  • 1. Unit Testing Framework for JavaScript Alok Guha
  • 2.
  • 3. 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
  • 4. 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.
  • 5. Jasmine A 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.
  • 7. 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
  • 8. 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
  • 9. beforeEach & afterEach • Both takes a function which executes before and after execution of each spec.
  • 10. 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.
  • 12. 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.
  • 13. Lets welcome Jasmine • Live examples
  • 14. references • http://pivotal.github.com/jasmine/ • http://evanhahn.com/how-do-i-jasmine/ • http://try-jasmine.heroku.com/ • My own experiences.

Notas do Editor

  1. Where to start in the processWhat to test and what not to testHow much to test in one goWhat to call the testsHow to understand why a test failsdefine a test set for the unit first;then implement the unit;finally verify that the implementation of the unit makes the tests succeed.