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

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
 

Similar to Angular JS Unit Testing - Overview (20)

PDF
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
PPTX
Slaven tomac unit testing in angular js
Slaven Tomac
 
PDF
Angularjs Test Driven Development (TDD)
Anis Bouhachem Djer
 
PDF
Angular Application Testing
Troy Miles
 
PPTX
Front end development with Angular JS
Bipin
 
PDF
Testing AngularJS
Jacopo Nardiello
 
PDF
An Introduction To Testing In AngularJS Applications
Rohan Chandane
 
PDF
Angular Intermediate
LinkMe Srl
 
PDF
Unit Testing in Angular
Knoldus Inc.
 
PDF
Angular Testing
Kourosh Sajjadi
 
PPTX
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
murtazahaveliwala
 
PDF
How Testability Inspires AngularJS Design / Ran Mizrahi
Ran Mizrahi
 
PPTX
Angular Unit testing.pptx
RiyaBangera
 
PDF
Ultimate Introduction To AngularJS
Jacopo Nardiello
 
PDF
AngularJS Beginner Day One
Troy Miles
 
PDF
Workshop 14: AngularJS Parte III
Visual Engineering
 
PDF
Describe's Full of It's
Jim Lynch
 
PDF
Testing Angular apps_ A complete guide for developers.pdf
Peerbits
 
PPTX
AngularJS Introduction (Talk given on Aug 5 2013)
Abhishek Anand
 
PPT
AngularJS Testing Strategies
njpst8
 
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Slaven tomac unit testing in angular js
Slaven Tomac
 
Angularjs Test Driven Development (TDD)
Anis Bouhachem Djer
 
Angular Application Testing
Troy Miles
 
Front end development with Angular JS
Bipin
 
Testing AngularJS
Jacopo Nardiello
 
An Introduction To Testing In AngularJS Applications
Rohan Chandane
 
Angular Intermediate
LinkMe Srl
 
Unit Testing in Angular
Knoldus Inc.
 
Angular Testing
Kourosh Sajjadi
 
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
murtazahaveliwala
 
How Testability Inspires AngularJS Design / Ran Mizrahi
Ran Mizrahi
 
Angular Unit testing.pptx
RiyaBangera
 
Ultimate Introduction To AngularJS
Jacopo Nardiello
 
AngularJS Beginner Day One
Troy Miles
 
Workshop 14: AngularJS Parte III
Visual Engineering
 
Describe's Full of It's
Jim Lynch
 
Testing Angular apps_ A complete guide for developers.pdf
Peerbits
 
AngularJS Introduction (Talk given on Aug 5 2013)
Abhishek Anand
 
AngularJS Testing Strategies
njpst8
 
Ad

Recently uploaded (20)

PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
DOCX
Import Data Form Excel to Tally Services
Tally xperts
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Human Resources Information System (HRIS)
Amity University, Patna
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Import Data Form Excel to Tally Services
Tally xperts
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Ad

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() }));