SlideShare a Scribd company logo
U N I T - T E S T I N G
A N G U L A R J S
I N P R A C T I C E
• Karma
• Jasmine
• About
• Review a Controller Spec Demo
• Angular Mocks
A N G U L A R U N I T T E S T I N G
• The goal is to test small units of code by isolating it from the rest of
the code to determine whether it behaves exactly as we expected
• Testing inspires correct AngularJS design and help us to avoid
mixing concerns in one piece of the code which does everything
(separate controller logic from DOM by using directives and BL in
services)
• Angular's built-in DI makes testing easier by stubbing or mocking a
component's dependencies, without having to mess with any
global variable that could affect another test
• Karma is a NodeJS command line test runner which spawns a web
server to load the application's source code and execute the tests
in a launched browser instance (or multiple different browser
instances) to see if the tests pass under different browser
environments
• Karma is executed on the command line and will display the
results of your tests on the command line once they have run in
the browser.
• It makes no specific assumptions about how you write your tests
• Jasmine is a behavior driven development framework for testing
javascript code
• Jasmine provides functions to help with structuring tests and also
making assertions
• It does not rely on:
• Browsers
• DOM
• Any JS framework
J A S M I N E 1 0 1
• We begin by creating a test suite with the global describe function that
wrap our specs. it takes two arguments, a string and a function
• We define a spec by the global function it which also takes two
arguments, a string description and a function that contains at least one
expectations
• We use the expect function chained to a matcher function implementing
a boolean comparison between the actual value and the expected value
• A test with all true expectations is considered a passing test, whereas a
spec with one or more expectations that evaluate to false is considered a
failing test
Q U I C K I N T R O D U C T I O N
M A T C H E R S
• toEqual(val) not.toEqual(val)
• toBe(val) not.toBe(val)
• toMatch(pattern) not.toMatch(pattern)
• toBeDefined() ...
• toBeUndefined()
• toBeNull()
• toBeTruthy()
• toContain(val)
• toBeGreaterThen(val)
• toThrow(exp)
• Rather than setting up our test conditions manually in every test,
we can use the beforeEach (afterEach) method to run a group of
setup functions. The beforeEach() function takes a single
argument: a function that is called once before each spec is run. It
can be used in a describe block, like so:
S P I E S
• A spy can stub any function and tracks calls to it and all arguments.
• A spy only exists in the describe or it block it is defined, and will be
removed after each spec.
• There are special matchers for interacting with spies.
• The toHaveBeenCalled matcher will return true if the spy was called.
• The toHaveBeenCalledWith matcher will return true if the argument list matches
any of the recored calls to the spy
Mocks vs Spies vs Stubs vs Fixtures
• Mocks replace entire objects/interfaces to control data flow
• Spies replace/patch existing functions to intercept behavior
• Stubs hijack the return value of a function to control program flow
• Fixtures holds a set of sample data returned from a mocked
backend request
• AngularJS has mocks built-in. Spies, stubs and fixtures can be
used with javascript
A N G U L A R M O C K S
• Angular also provides the ngMock module, which provides
mocking for your tests. This is used to inject and mock Angular
services within unit tests.
• ngMock also extends various core ng services so they are
synchronous. Having tests synchronous keeps them much cleaner
and easier to work with. One of the most useful parts of ngMock is
$httpBackend, which lets us mock XHR requests in tests, and
return sample data instead. (fixtures)
• $httpBackend
• $controller
• $interval
• $timeout
Test an angular controller example:
Jasmine & ngMock
Angular Unit Testing

More Related Content

What's hot (20)

PPTX
Unit testing JavaScript: Jasmine & karma intro
Maurice De Beijer [MVP]
 
PDF
Angular Application Testing
Troy Miles
 
ODP
Unit Testing and Coverage for AngularJS
Knoldus Inc.
 
PPTX
Testing React Applications
stbaechler
 
ODP
Angular JS Unit Testing - Overview
Thirumal Sakthivel
 
PDF
Angularjs - Unit testing introduction
Nir Kaufman
 
PDF
Intro to Unit Testing in AngularJS
Jim Lynch
 
PPT
Testing in AngularJS
Peter Drinnan
 
PDF
Automated Testing in Angular Slides
Jim Lynch
 
ODP
Jquery- One slide completing all JQuery
Knoldus Inc.
 
PDF
AngularJS Unit Testing w/Karma and Jasmine
foxp2code
 
PDF
AngularJS Unit Test
Chiew Carol
 
PDF
Quick tour to front end unit testing using jasmine
Gil Fink
 
PDF
Test-Driven Development of AngularJS Applications
FITC
 
PDF
Unit-testing and E2E testing in JS
Michael Haberman
 
PPTX
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky
 
PPTX
AngularJS Unit Testing
Prince Norin
 
PDF
Intro to testing Javascript with jasmine
Timothy Oxley
 
PDF
Javascript tdd byandreapaciolla
Andrea Paciolla
 
PDF
Karma - JS Test Runner
Sebastiano Armeli
 
Unit testing JavaScript: Jasmine & karma intro
Maurice De Beijer [MVP]
 
Angular Application Testing
Troy Miles
 
Unit Testing and Coverage for AngularJS
Knoldus Inc.
 
Testing React Applications
stbaechler
 
Angular JS Unit Testing - Overview
Thirumal Sakthivel
 
Angularjs - Unit testing introduction
Nir Kaufman
 
Intro to Unit Testing in AngularJS
Jim Lynch
 
Testing in AngularJS
Peter Drinnan
 
Automated Testing in Angular Slides
Jim Lynch
 
Jquery- One slide completing all JQuery
Knoldus Inc.
 
AngularJS Unit Testing w/Karma and Jasmine
foxp2code
 
AngularJS Unit Test
Chiew Carol
 
Quick tour to front end unit testing using jasmine
Gil Fink
 
Test-Driven Development of AngularJS Applications
FITC
 
Unit-testing and E2E testing in JS
Michael Haberman
 
Unit testing in JavaScript with Jasmine and Karma
Andrey Kolodnitsky
 
AngularJS Unit Testing
Prince Norin
 
Intro to testing Javascript with jasmine
Timothy Oxley
 
Javascript tdd byandreapaciolla
Andrea Paciolla
 
Karma - JS Test Runner
Sebastiano Armeli
 

Similar to Angular Unit Testing (20)

PDF
An Introduction To Testing In AngularJS Applications
Rohan Chandane
 
PDF
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
 
PDF
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
PDF
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 
PPTX
Angular Unit testing.pptx
RiyaBangera
 
PPTX
jasmine
Asanka Indrajith
 
PPTX
Slaven tomac unit testing in angular js
Slaven Tomac
 
PDF
JavaScript Unit Testing with an Angular 5.x Use Case 101
Hazem Saleh
 
PPTX
Javascript Testing with Jasmine 101
Roy Yu
 
PDF
Angular Testing
Kourosh Sajjadi
 
PDF
Angularjs Test Driven Development (TDD)
Anis Bouhachem Djer
 
PDF
Js fwdays unit tesing javascript(by Anna Khabibullina)
Anna Khabibullina
 
PPTX
JS Frameworks Day April,26 of 2014
DA-14
 
PDF
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
PDF
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
PPTX
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
PDF
Unit Testing in Angular
Knoldus Inc.
 
PDF
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
Anup Singh
 
PDF
Javascript Unit Testing Tools
PixelCrayons
 
PDF
Quick tour to front end unit testing using jasmine
Gil Fink
 
An Introduction To Testing In AngularJS Applications
Rohan Chandane
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
 
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 
Angular Unit testing.pptx
RiyaBangera
 
Slaven tomac unit testing in angular js
Slaven Tomac
 
JavaScript Unit Testing with an Angular 5.x Use Case 101
Hazem Saleh
 
Javascript Testing with Jasmine 101
Roy Yu
 
Angular Testing
Kourosh Sajjadi
 
Angularjs Test Driven Development (TDD)
Anis Bouhachem Djer
 
Js fwdays unit tesing javascript(by Anna Khabibullina)
Anna Khabibullina
 
JS Frameworks Day April,26 of 2014
DA-14
 
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
Unit Testing in Angular
Knoldus Inc.
 
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
Anup Singh
 
Javascript Unit Testing Tools
PixelCrayons
 
Quick tour to front end unit testing using jasmine
Gil Fink
 
Ad

Angular Unit Testing

  • 1. U N I T - T E S T I N G A N G U L A R J S I N P R A C T I C E • Karma • Jasmine • About • Review a Controller Spec Demo • Angular Mocks
  • 2. A N G U L A R U N I T T E S T I N G • The goal is to test small units of code by isolating it from the rest of the code to determine whether it behaves exactly as we expected • Testing inspires correct AngularJS design and help us to avoid mixing concerns in one piece of the code which does everything (separate controller logic from DOM by using directives and BL in services) • Angular's built-in DI makes testing easier by stubbing or mocking a component's dependencies, without having to mess with any global variable that could affect another test
  • 3. • Karma is a NodeJS command line test runner which spawns a web server to load the application's source code and execute the tests in a launched browser instance (or multiple different browser instances) to see if the tests pass under different browser environments • Karma is executed on the command line and will display the results of your tests on the command line once they have run in the browser. • It makes no specific assumptions about how you write your tests
  • 4. • Jasmine is a behavior driven development framework for testing javascript code • Jasmine provides functions to help with structuring tests and also making assertions • It does not rely on: • Browsers • DOM • Any JS framework
  • 5. J A S M I N E 1 0 1 • We begin by creating a test suite with the global describe function that wrap our specs. it takes two arguments, a string and a function • We define a spec by the global function it which also takes two arguments, a string description and a function that contains at least one expectations • We use the expect function chained to a matcher function implementing a boolean comparison between the actual value and the expected value • A test with all true expectations is considered a passing test, whereas a spec with one or more expectations that evaluate to false is considered a failing test Q U I C K I N T R O D U C T I O N
  • 6. M A T C H E R S • toEqual(val) not.toEqual(val) • toBe(val) not.toBe(val) • toMatch(pattern) not.toMatch(pattern) • toBeDefined() ... • toBeUndefined() • toBeNull() • toBeTruthy() • toContain(val) • toBeGreaterThen(val) • toThrow(exp)
  • 7. • Rather than setting up our test conditions manually in every test, we can use the beforeEach (afterEach) method to run a group of setup functions. The beforeEach() function takes a single argument: a function that is called once before each spec is run. It can be used in a describe block, like so:
  • 8. S P I E S • A spy can stub any function and tracks calls to it and all arguments. • A spy only exists in the describe or it block it is defined, and will be removed after each spec. • There are special matchers for interacting with spies. • The toHaveBeenCalled matcher will return true if the spy was called. • The toHaveBeenCalledWith matcher will return true if the argument list matches any of the recored calls to the spy
  • 9. Mocks vs Spies vs Stubs vs Fixtures • Mocks replace entire objects/interfaces to control data flow • Spies replace/patch existing functions to intercept behavior • Stubs hijack the return value of a function to control program flow • Fixtures holds a set of sample data returned from a mocked backend request • AngularJS has mocks built-in. Spies, stubs and fixtures can be used with javascript
  • 10. A N G U L A R M O C K S • Angular also provides the ngMock module, which provides mocking for your tests. This is used to inject and mock Angular services within unit tests. • ngMock also extends various core ng services so they are synchronous. Having tests synchronous keeps them much cleaner and easier to work with. One of the most useful parts of ngMock is $httpBackend, which lets us mock XHR requests in tests, and return sample data instead. (fixtures) • $httpBackend • $controller • $interval • $timeout
  • 11. Test an angular controller example: Jasmine & ngMock