javascript Unit Tests with
Angular 1.x
Ron Apelbaum
AppPulse, HPE
July 2016
Agenda
• javascript dangers
• Testable code
Motivation
• Test framework
Jasmine
• Understand angular 1.x
Angular unit test
• Karma, IntelliJ
Real world
2
3
github.com/ronapelbaum/angular-jasmine
Software Testing
4
Code Quality
Functionality
Unit testing
5
Given
When
Then
6
When
<code>
Then
Given
javascript: welcome to runtime errors
7
Scripting language
8
jsFiddle
Modular Coding
9
jsFiddle
JavaScript Unit Testing
10
1 Runtime Compilation
2
3
Modular coding
Very Fast
Questions?
11
Jasmine introduction
12
Specs (1)
13
14
Specs (2)
15
toBe() || not.toBe()
16
Matchers (2): toEqual
17
Matchers (3)
18
Matchers (4)
19
20
1. test a function
2. test an object
3. testable code
Questions?
21
22
23
Spies (1): spyOn
Spy on an existing object member
Spies (2): spyOn
24
Spies (3): createSpy
25
Spies (4): createSpyObj
26
Create an object and Spy on ‘virtual’ members
27
1. Testable code?
2. spyOn
3. createSpyObj
4. CallFake
Questions?
28
Jasmine + angular
29
30
Constructor definition:
Usage:
New()
31
Constructor definition:
Usage:
DI definition:
Angular 101
angular
angular.module()
$injector
32
ngMock
angular.mock.module()
angular.mock.inject()
33
34
Services
test a service
35
namespace
get instance
test a service:
inject() + spyOn()
36
spyOn the instance from DI
37
1. Test a service
2. inject () + spyOn()
$provide
Registering components with the $injector
angular.module(...).service(...)
angular.module(...).factory(...)
38
test a service:
createSpyObj() + $provide
39
Create another module
Override namespace
Gets mock instance
40
createSpyObj() + $provide()
Questions?
41
42
Controllers
test a controller
43
$controller
test
test a controller ($scope)
44
$controller
test
$rootScope
DI
45
1. Stateless controller
2. Controller as
3. $scope
46
Directives
$compile
Compiles DOM into a template and produces a
template function
47
test a directive
48
Create element
test
$compile with scope
Change scope
Test element
49
1. Directive + attrs
2. Directive + scope + watch
50
Patterns
51
1. $http
2. $timeout
3. Events
4. Promises
Real Life (demo)
52
Summary
Core Angular
• Service
• Controller
• Directive
Patters
• $http
• $watch
• $q
• Events
Real Life
• Karma
• Debug
• coverage
53
Links
 JsFiddle examples:
https://github.com/ronapelbaum/angular-jasmine
 Summary exercise:
https://github.com/ronapelbaum/mangal
 My Blog:
https://ronapelbaum.wordpress.com/tag/angular-jasmine
54
Thank you
ron.apelbaum@hpe.com
55

Javascript unit tests with angular 1.x

Editor's Notes

  • #2 Who am I Who are you
  • #4 Questions?
  • #6 state machine black box
  • #8 Motivation
  • #9 no compile runtime Static analysis
  • #10 jsFiddle
  • #11  Questions?
  • #13 Jasmine.github.io
  • #14 Describe = spec (not test) It (...should)
  • #15 beforeEach
  • #16 Matchers.
  • #19 Other matchers
  • #20 Other matchers Questions?
  • #21 Questions?
  • #23 Spies.
  • #24 spyOn = exist object Special matchers
  • #25 “and”
  • #27 Questions?
  • #31 new
  • #33 Module === namespace injects instances
  • #34 Module === namespace for mock injector Inject – get the instance
  • #37  Questions?
  • #38 Test service with DI https://jsfiddle.net/ronapelbaum/2fb53hbr/ spyOn DI https://jsfiddle.net/ronapelbaum/2fb53hbr/1/ NOTE: the service constructor has been called!
  • #39 Core angular
  • #40 Questions?
  • #41 Real DI NOTE: module must be before inject NOTE: constructor NOT called
  • #44 $controller - angular Service – singleton Controller – not singleton
  • #45 We are testing the $scope We can inject dependencies Questions?
  • #46 Questions?
  • #48 Core angular
  • #49 Questions?
  • #52 Questions?