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
PDF
Angular testing
PDF
Angularjs - Unit testing introduction
PPTX
AngularJS Unit Testing
PDF
AngularJS Unit Testing w/Karma and Jasmine
PDF
Test-Driven Development of AngularJS Applications
PDF
Intro to Unit Testing in AngularJS
PPTX
Unit testing of java script and angularjs application using Karma Jasmine Fra...
Angular Testing
Angular testing
Angularjs - Unit testing introduction
AngularJS Unit Testing
AngularJS Unit Testing w/Karma and Jasmine
Test-Driven Development of AngularJS Applications
Intro to Unit Testing in AngularJS
Unit testing of java script and angularjs application using Karma Jasmine Fra...

What's hot (20)

ODP
Unit Testing and Coverage for AngularJS
PPTX
Unit testing in JavaScript with Jasmine and Karma
PDF
AngularJS Unit Test
PPT
Testing in AngularJS
PDF
Karma - JS Test Runner
PDF
Intro to testing Javascript with jasmine
PDF
Advanced Jasmine - Front-End JavaScript Unit Testing
ODP
Jquery- One slide completing all JQuery
PDF
Quick tour to front end unit testing using jasmine
PDF
Jasmine BDD for Javascript
PPTX
Unit testing JavaScript: Jasmine & karma intro
PPTX
Angular Unit Testing
PPT
Testing Javascript with Jasmine
PDF
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
PDF
JavaScript TDD with Jasmine and Karma
PDF
Javascript tdd byandreapaciolla
PPTX
Angular Unit Testing
PDF
Unit testing JavaScript using Mocha and Node
PDF
Unit Testing Express and Koa Middleware in ES2015
PDF
Unit testing with mocha
Unit Testing and Coverage for AngularJS
Unit testing in JavaScript with Jasmine and Karma
AngularJS Unit Test
Testing in AngularJS
Karma - JS Test Runner
Intro to testing Javascript with jasmine
Advanced Jasmine - Front-End JavaScript Unit Testing
Jquery- One slide completing all JQuery
Quick tour to front end unit testing using jasmine
Jasmine BDD for Javascript
Unit testing JavaScript: Jasmine & karma intro
Angular Unit Testing
Testing Javascript with Jasmine
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript TDD with Jasmine and Karma
Javascript tdd byandreapaciolla
Angular Unit Testing
Unit testing JavaScript using Mocha and Node
Unit Testing Express and Koa Middleware in ES2015
Unit testing with mocha
Ad

Similar to Angular JS Unit Testing - Overview (20)

PDF
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
PPTX
Slaven tomac unit testing in angular js
PDF
Angularjs Test Driven Development (TDD)
PDF
Angular Application Testing
PPTX
Front end development with Angular JS
PDF
Testing AngularJS
PDF
An Introduction To Testing In AngularJS Applications
PDF
Angular Intermediate
PDF
Unit Testing in Angular
PDF
Angular Testing
PPTX
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
PDF
How Testability Inspires AngularJS Design / Ran Mizrahi
PPTX
Angular Unit testing.pptx
PDF
Ultimate Introduction To AngularJS
PDF
AngularJS Beginner Day One
PDF
Workshop 14: AngularJS Parte III
PDF
Describe's Full of It's
PDF
Testing Angular apps_ A complete guide for developers.pdf
PPTX
AngularJS Introduction (Talk given on Aug 5 2013)
PPT
AngularJS Testing Strategies
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
Slaven tomac unit testing in angular js
Angularjs Test Driven Development (TDD)
Angular Application Testing
Front end development with Angular JS
Testing AngularJS
An Introduction To Testing In AngularJS Applications
Angular Intermediate
Unit Testing in Angular
Angular Testing
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
How Testability Inspires AngularJS Design / Ran Mizrahi
Angular Unit testing.pptx
Ultimate Introduction To AngularJS
AngularJS Beginner Day One
Workshop 14: AngularJS Parte III
Describe's Full of It's
Testing Angular apps_ A complete guide for developers.pdf
AngularJS Introduction (Talk given on Aug 5 2013)
AngularJS Testing Strategies
Ad

Recently uploaded (20)

PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
System and Network Administraation Chapter 3
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
AIRLINE PRICE API | FLIGHT API COST |
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
PDF
top salesforce developer skills in 2025.pdf
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
Become an Agentblazer Champion Challenge Kickoff
PDF
medical staffing services at VALiNTRY
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
Presentation of Computer CLASS 2 .pptx
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
A REACT POMODORO TIMER WEB APPLICATION.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
System and Network Administraation Chapter 3
PTS Company Brochure 2025 (1).pdf.......
AIRLINE PRICE API | FLIGHT API COST |
Online Work Permit System for Fast Permit Processing
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
top salesforce developer skills in 2025.pdf
ManageIQ - Sprint 268 Review - Slide Deck
Become an Agentblazer Champion Challenge Kickoff
medical staffing services at VALiNTRY
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
The Role of Automation and AI in EHS Management for Data Centers.pdf
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Odoo POS Development Services by CandidRoot Solutions
ISO 45001 Occupational Health and Safety Management System
Presentation of Computer CLASS 2 .pptx
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
2025 Textile ERP Trends: SAP, Odoo & Oracle
A REACT POMODORO TIMER WEB APPLICATION.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...

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