55
javascript Unit Tests with Angular 1.x Ron Apelbaum AppPulse, HPE July 2016

Javascript unit tests with angular 1.x

Embed Size (px)

Citation preview

Page 1: Javascript unit tests with angular 1.x

javascript Unit Tests with Angular 1.x

Ron ApelbaumAppPulse, HPE

July 2016

Page 2: Javascript unit tests with angular 1.x

2

Agenda• javascript dangers• Testable code

Motivation

• Test framework

Jasmine

• Understand angular 1.x

Angular unit test

• Karma, IntelliJReal world

Page 3: Javascript unit tests with angular 1.x

3

github.com/ronapelbaum/angular-jasmine

Page 4: Javascript unit tests with angular 1.x

4

Software TestingE2E

Component

Unit TestCode Quality

Functionality

Page 5: Javascript unit tests with angular 1.x

5

Unit testingGiven

When

Then

Page 6: Javascript unit tests with angular 1.x

6

When

<code>

Then

Given

Page 7: Javascript unit tests with angular 1.x

7

javascript: welcome to runtime errors

Page 8: Javascript unit tests with angular 1.x

8

Scripting languageerror

???

jsFiddle

Page 9: Javascript unit tests with angular 1.x

9

Modular Coding

jsFiddle

Page 10: Javascript unit tests with angular 1.x

10

JavaScript Unit Testing1 Runtime Compilation

2

3

Modular coding

Very Fast

Page 11: Javascript unit tests with angular 1.x

11

Questions?

Page 12: Javascript unit tests with angular 1.x

12

Jasmine introduction

Page 13: Javascript unit tests with angular 1.x

13

Specs (1)

Page 14: Javascript unit tests with angular 1.x

14

Specs (2)

Page 15: Javascript unit tests with angular 1.x

15

Page 16: Javascript unit tests with angular 1.x

16

toBe() || not.toBe()

Page 17: Javascript unit tests with angular 1.x

17

Matchers (2): toEqual

Page 18: Javascript unit tests with angular 1.x

18

Matchers (3)

Page 19: Javascript unit tests with angular 1.x

19

Matchers (4)

Page 20: Javascript unit tests with angular 1.x

20

1. test a function

2. test an object

3. testable code

Page 21: Javascript unit tests with angular 1.x

21

Questions?

Page 22: Javascript unit tests with angular 1.x

22

Page 23: Javascript unit tests with angular 1.x

23

Spies (1): spyOnSpy on an existing object member

Page 24: Javascript unit tests with angular 1.x

24

Spies (2): spyOn

Page 25: Javascript unit tests with angular 1.x

25

Spies (3): createSpy

Page 26: Javascript unit tests with angular 1.x

26

Spies (4): createSpyObj

Create an object and Spy on ‘virtual’ members

Page 28: Javascript unit tests with angular 1.x

28

Questions?

Page 29: Javascript unit tests with angular 1.x

29

Jasmine + angular

Page 30: Javascript unit tests with angular 1.x

30

Constructor definition:

Usage:New()

Page 31: Javascript unit tests with angular 1.x

31

Constructor definition:

Usage:

DI definition:Where is new()?

Angular 101

Page 32: Javascript unit tests with angular 1.x

32

angular

angular.module()

$injector

Page 33: Javascript unit tests with angular 1.x

33

ngMock

angular.mock.module()

angular.mock.inject()

Page 34: Javascript unit tests with angular 1.x

34

Services

Page 35: Javascript unit tests with angular 1.x

35

test a service namespace

get instance

Page 36: Javascript unit tests with angular 1.x

36

test a service: inject() + spyOn()

spyOn the instance from DI

Page 38: Javascript unit tests with angular 1.x

38

$provide

Registering components with the $injector

angular.module(...).service(...)

angular.module(...).factory(...)

Page 39: Javascript unit tests with angular 1.x

39

test a service: createSpyObj() + $provide

Create another module

Override namespace

Gets mock instance

Page 40: Javascript unit tests with angular 1.x

40

createSpyObj() + $provide()

Page 41: Javascript unit tests with angular 1.x

41

Questions?

Page 42: Javascript unit tests with angular 1.x

42

Controllers

Page 43: Javascript unit tests with angular 1.x

43

test a controller$controller

test

Page 44: Javascript unit tests with angular 1.x

44

test a controller ($scope)$controller

test

$rootScope

DI

Page 45: Javascript unit tests with angular 1.x

45

1. Stateless controller

2. Controller as

3. $scope

Page 46: Javascript unit tests with angular 1.x

46

Directives

Page 47: Javascript unit tests with angular 1.x

47

$compile

Compiles DOM into a template and produces a template function

Page 48: Javascript unit tests with angular 1.x

48

test a directive Create element

test

$compile with scope

Change scopeTest element

Page 50: Javascript unit tests with angular 1.x

50

Patterns

Page 52: Javascript unit tests with angular 1.x

52

Real Life (demo)

Page 53: Javascript unit tests with angular 1.x

53

Summary

Core Angular

• Service

• Controller

• Directive

Patters

• $http• $watch• $q• Events

Real Life

• Karma

• Debug

• coverage