SlideShare a Scribd company logo
Angular JS Unit Testing
By,
Thirumal S.
Agenda
●
What is karma and Jasmine
●
Jasmine Matchers
●
Testing Angular Routes
●
Testing Angular Controller
●
Testing Angular Directive
What is Karma?
●
Karma is a JavaScript command line tool
that can be used to spawn a web server
which loads your application's source code
and executes your tests.
●
Karma is a NodeJS application, and should
be installed through npm.
What is Jasmine?
●
Jasmine is a test driven development
framework for JavaScript that has
become the most popular choice for
testing Angular applications.
●
Jasmine provides functions to help with
structuring your tests and also making
assertions.
Jasmine Matchers
●
toEqual checks for equality, not
necessarily the same object.
Var a=10;
expect(a).toEqual(10)
●
toBe checks if two objects are the same.
Var a=10,b=10;
expect(a).toBe(b)
●
toBeTruthy checks if a value is truthy (not just true ).
●
toBeFalsy checks if a value is falsy (not just false ).
●
toContain checks if a value is inside another.
Var a= “Hello World”;
expect(a).toContain(“World”)
●
toBeDefined checks if a value is defined.
●
toBeUndefined checks if a value is undefined.
●
toBeNull checks if a value is null.
●
toBeCloseTo checks decimal proximity.
●
For More About Jasmine Matchers
https://github.com/JamieMason/Jasmine-Matchers
Angular Testing
Controllers// get the module that contain the controller
beforeEach(module('todolist'));
// inject the $controller and the rootScope
beforeEach(inject(function ($rootScope, $controller) {
// create a fresh new scope for the controller
scope = $rootScope.$new();
// create a controller with this scope
ctrl = $controller('TodoListController',{$scope: scope});
}));
Angular Testing Routes
/ get the module that contain the controller
beforeEach(module('todolist'));
// inject the $State and the rootScope
beforeEach(inject(function ($rootScope, $state, $templateCache_) {
// create a fresh new scope for the controller
scope = $rootScope.$new();
state = $state;
template = $templateCache;
// create a controller with this scope
ctrl = $controller('TodoListController',{$scope: scope});
}));
Angular Testing Services
// get the module that contain the service
beforeEach(module('todolist'));
// inject the $injector
beforeEach(inject(function ($injector) {
// use the $injector to get a hold on the service
service = $injector.get(‘ServiceName’);
}));
Angular Testing
Directives// get the module that contain the service
beforeEach(module('todolist'));
// inject the $compile service and the $rootScope
beforeEach(inject(function ($compile, $rootScope) {
// use the $rootScope to create a scope for the directive
scope = $rootScope;
// create an angular element from a HTML string
element = angular.element(‘<div my-directive ></div>’)
// compile the element with the scope
$compile(element)(scope)
scope.$apply()
}));
Thank you.

More Related Content

PDF
Angular Testing
Priscila Negreiros
 
PDF
Angular testing
Raissa Ferreira
 
PDF
Angularjs - Unit testing introduction
Nir Kaufman
 
PPTX
AngularJS Unit Testing
Prince Norin
 
PDF
AngularJS Unit Testing w/Karma and Jasmine
foxp2code
 
PDF
Test-Driven Development of AngularJS Applications
FITC
 
PDF
Intro to Unit Testing in AngularJS
Jim Lynch
 
PPTX
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Samyak Bhalerao
 
Angular Testing
Priscila Negreiros
 
Angular testing
Raissa Ferreira
 
Angularjs - Unit testing introduction
Nir Kaufman
 
AngularJS Unit Testing
Prince Norin
 
AngularJS Unit Testing w/Karma and Jasmine
foxp2code
 
Test-Driven Development of AngularJS Applications
FITC
 
Intro to Unit Testing in AngularJS
Jim Lynch
 
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Samyak Bhalerao
 

What's hot (20)

ODP
Unit Testing and Coverage for AngularJS
Knoldus Inc.
 
PPTX
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky
 
PDF
AngularJS Unit Test
Chiew Carol
 
PPT
Testing in AngularJS
Peter Drinnan
 
PDF
Karma - JS Test Runner
Sebastiano Armeli
 
PDF
Intro to testing Javascript with jasmine
Timothy Oxley
 
PDF
Advanced Jasmine - Front-End JavaScript Unit Testing
Lars Thorup
 
ODP
Jquery- One slide completing all JQuery
Knoldus Inc.
 
PDF
Quick tour to front end unit testing using jasmine
Gil Fink
 
PDF
Jasmine BDD for Javascript
Luis Alfredo Porras Páez
 
PPTX
Unit testing JavaScript: Jasmine & karma intro
Maurice De Beijer [MVP]
 
PPTX
Angular Unit Testing
Avi Engelshtein
 
PPT
Testing Javascript with Jasmine
Tim Tyrrell
 
PDF
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
 
PDF
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 
PDF
Javascript tdd byandreapaciolla
Andrea Paciolla
 
PPTX
Angular Unit Testing
Alessandro Giorgetti
 
PDF
Unit testing JavaScript using Mocha and Node
Josh Mock
 
PDF
Unit Testing Express and Koa Middleware in ES2015
Morris Singer
 
PDF
Unit testing with mocha
Revath S Kumar
 
Unit Testing and Coverage for AngularJS
Knoldus Inc.
 
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky
 
AngularJS Unit Test
Chiew Carol
 
Testing in AngularJS
Peter Drinnan
 
Karma - JS Test Runner
Sebastiano Armeli
 
Intro to testing Javascript with jasmine
Timothy Oxley
 
Advanced Jasmine - Front-End JavaScript Unit Testing
Lars Thorup
 
Jquery- One slide completing all JQuery
Knoldus Inc.
 
Quick tour to front end unit testing using jasmine
Gil Fink
 
Jasmine BDD for Javascript
Luis Alfredo Porras Páez
 
Unit testing JavaScript: Jasmine & karma intro
Maurice De Beijer [MVP]
 
Angular Unit Testing
Avi Engelshtein
 
Testing Javascript with Jasmine
Tim Tyrrell
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
 
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 
Javascript tdd byandreapaciolla
Andrea Paciolla
 
Angular Unit Testing
Alessandro Giorgetti
 
Unit testing JavaScript using Mocha and Node
Josh Mock
 
Unit Testing Express and Koa Middleware in ES2015
Morris Singer
 
Unit testing with mocha
Revath S Kumar
 
Ad

Similar to Angular JS Unit Testing - Overview (20)

PDF
Ultimate Introduction To AngularJS
Jacopo Nardiello
 
PDF
Testing AngularJS
Jacopo Nardiello
 
PPTX
Slaven tomac unit testing in angular js
Slaven Tomac
 
PDF
Describe's Full of It's
Jim Lynch
 
PPT
Automated javascript unit testing
ryan_chambers
 
PDF
Angularjs Test Driven Development (TDD)
Anis Bouhachem Djer
 
PPT
Test strategy for web development
alice yang
 
PDF
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
PDF
An Introduction To Testing In AngularJS Applications
Rohan Chandane
 
PDF
Quick tour to front end unit testing using jasmine
Gil Fink
 
PDF
How Testability Inspires AngularJS Design / Ran Mizrahi
Ran Mizrahi
 
ODP
AngularJs Crash Course
Keith Bloomfield
 
PPTX
Good karma: UX Patterns and Unit Testing in Angular with Karma
ExoLeaders.com
 
PDF
Test driven node.js
Jay Harris
 
PPTX
Angular js
Behind D Walls
 
PDF
JavaScript Unit Testing with an Angular 5.x Use Case 101
Hazem Saleh
 
PPTX
jasmine
Asanka Indrajith
 
PDF
Automated acceptance test
Bryan Liu
 
PDF
Test-driven Development with AEM
Jan Wloka
 
PDF
Opinionated AngularJS
prabhutech
 
Ultimate Introduction To AngularJS
Jacopo Nardiello
 
Testing AngularJS
Jacopo Nardiello
 
Slaven tomac unit testing in angular js
Slaven Tomac
 
Describe's Full of It's
Jim Lynch
 
Automated javascript unit testing
ryan_chambers
 
Angularjs Test Driven Development (TDD)
Anis Bouhachem Djer
 
Test strategy for web development
alice yang
 
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
An Introduction To Testing In AngularJS Applications
Rohan Chandane
 
Quick tour to front end unit testing using jasmine
Gil Fink
 
How Testability Inspires AngularJS Design / Ran Mizrahi
Ran Mizrahi
 
AngularJs Crash Course
Keith Bloomfield
 
Good karma: UX Patterns and Unit Testing in Angular with Karma
ExoLeaders.com
 
Test driven node.js
Jay Harris
 
Angular js
Behind D Walls
 
JavaScript Unit Testing with an Angular 5.x Use Case 101
Hazem Saleh
 
Automated acceptance test
Bryan Liu
 
Test-driven Development with AEM
Jan Wloka
 
Opinionated AngularJS
prabhutech
 
Ad

Recently uploaded (20)

PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
Become an Agentblazer Champion Challenge
Dele Amefo
 
PDF
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
PDF
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PDF
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
PPTX
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
Become an Agentblazer Champion Challenge
Dele Amefo
 
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 

Angular JS Unit Testing - Overview

  • 1. Angular JS Unit Testing By, Thirumal S.
  • 2. Agenda ● What is karma and Jasmine ● Jasmine Matchers ● Testing Angular Routes ● Testing Angular Controller ● Testing Angular Directive
  • 3. What is Karma? ● Karma is a JavaScript command line tool that can be used to spawn a web server which loads your application's source code and executes your tests. ● Karma is a NodeJS application, and should be installed through npm.
  • 4. What is Jasmine? ● Jasmine is a test driven development framework for JavaScript that has become the most popular choice for testing Angular applications. ● Jasmine provides functions to help with structuring your tests and also making assertions.
  • 5. Jasmine Matchers ● toEqual checks for equality, not necessarily the same object. Var a=10; expect(a).toEqual(10) ● toBe checks if two objects are the same. Var a=10,b=10; expect(a).toBe(b)
  • 6. ● toBeTruthy checks if a value is truthy (not just true ). ● toBeFalsy checks if a value is falsy (not just false ). ● toContain checks if a value is inside another. Var a= “Hello World”; expect(a).toContain(“World”) ● toBeDefined checks if a value is defined. ● toBeUndefined checks if a value is undefined. ● toBeNull checks if a value is null. ● toBeCloseTo checks decimal proximity. ● For More About Jasmine Matchers https://github.com/JamieMason/Jasmine-Matchers
  • 7. Angular Testing Controllers// get the module that contain the controller beforeEach(module('todolist')); // inject the $controller and the rootScope beforeEach(inject(function ($rootScope, $controller) { // create a fresh new scope for the controller scope = $rootScope.$new(); // create a controller with this scope ctrl = $controller('TodoListController',{$scope: scope}); }));
  • 8. Angular Testing Routes / get the module that contain the controller beforeEach(module('todolist')); // inject the $State and the rootScope beforeEach(inject(function ($rootScope, $state, $templateCache_) { // create a fresh new scope for the controller scope = $rootScope.$new(); state = $state; template = $templateCache; // create a controller with this scope ctrl = $controller('TodoListController',{$scope: scope}); }));
  • 9. Angular Testing Services // get the module that contain the service beforeEach(module('todolist')); // inject the $injector beforeEach(inject(function ($injector) { // use the $injector to get a hold on the service service = $injector.get(‘ServiceName’); }));
  • 10. Angular Testing Directives// get the module that contain the service beforeEach(module('todolist')); // inject the $compile service and the $rootScope beforeEach(inject(function ($compile, $rootScope) { // use the $rootScope to create a scope for the directive scope = $rootScope; // create an angular element from a HTML string element = angular.element(‘<div my-directive ></div>’) // compile the element with the scope $compile(element)(scope) scope.$apply() }));