Slide 1

Slide 1 text

UNIT TESTING

Slide 2

Slide 2 text

Ciro Nunes FRONTEND ENGINEER @cironunesdev

Slide 3

Slide 3 text

Belo Horizonte

Slide 4

Slide 4 text

Berlin

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

DO YOU DEVELOP WEB APPS?

Slide 8

Slide 8 text

DO THEY HAVE MORE THAN 10 FEATURES?

Slide 9

Slide 9 text

ARE YOU CONFIDENT REFACTORING YOUR CODE?

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

UNIT TESTS

Slide 13

Slide 13 text

UNIT TESTS are

Slide 14

Slide 14 text

UNIT TESTS automatic are

Slide 15

Slide 15 text

UNIT TESTS automatic fast are

Slide 16

Slide 16 text

UNIT TESTS automatic fast are documentation

Slide 17

Slide 17 text

1. Unit testing 101 2. Setup Angular testing environment 3. Test Angular services and components AGENDA

Slide 18

Slide 18 text

UNIT TESTING 101

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

expect(calc.add(10,.5)).toBe(15);

Slide 21

Slide 21 text

class.Calculator.{. ..add(firstNumber,.secondNumber).{. ....return.firstNumber.+.secondNumber;. ..}. } calculator.ts

Slide 22

Slide 22 text

import.{Calculator}.from.'./calculator';. describe('Calculator',.().=>.{. ..let.calc;. ... ..beforeEach(().=>.{. ....calc.=.new.Calculator();. ..});. ... ..it('sums.two.numbers',.().=>.{. ....expect(calc.add(2,.5)).toBe(7);. ..});. }); calculator.spec.ts

Slide 23

Slide 23 text

import.{Calculator}.from.'./calculator';. describe('Calculator',.().=>.{. ..let.calc;. ... ..beforeEach(().=>.{. ....calc.=.new.Calculator();. ..});. ... ..it('sums.two.numbers',.().=>.{. ....expect(calc.add(2,.5)).toBe(7);. ..});. }); calculator.spec.ts

Slide 24

Slide 24 text

import.{Calculator}.from.'./calculator';. describe('Calculator',.().=>.{. ..let.calc;. ... ..beforeEach(().=>.{. ....calc.=.new.Calculator();. ..});. ... ..it('sums.two.numbers',.().=>.{. ....expect(calc.add(2,.5)).toBe(7);. ..});. }); calculator.spec.ts

Slide 25

Slide 25 text

import.{Calculator}.from.'./calculator';. describe('Calculator',.().=>.{. ..let.calc;. ... ..beforeEach(().=>.{. ....calc.=.new.Calculator();. ..});. ... ..it('sums.two.numbers',.().=>.{. ....expect(calc.add(2,.5)).toBe(7);. ..});. }); calculator.spec.ts

Slide 26

Slide 26 text

import.{Calculator}.from.'./calculator';. describe('Calculator',.().=>.{. ..let.calc;. ... ..beforeEach(().=>.{. ....calc.=.new.Calculator();. ..});. ... ..it('sums.two.numbers',.().=>.{. ....expect(calc.add(2,.5)).toBe(7);. ..});. }); calculator.spec.ts Executed 1 of 1 SUCCESS

Slide 27

Slide 27 text

describe('Calculator',.().=>.{. ..describe('#add',.().=>.{});. ..describe('#subtract',.().=>.{});. ..describe('#multiply',.().=>.{});. ..describe('#divide',.().=>.{});. }); calculator.spec.ts http://betterspecs.org/

Slide 28

Slide 28 text

describe('Calculator',.().=>.{. ..beforeEach(...);. ..describe('#add',.().=>.{. ....it('sums.two.numbers',.().=>.{. ......expect(calc.add(2,.5)).toBe(7);. ....});. ....it('sums.three.numbers',.().=>.{. ......expect(calc.add(5,.10,.20)).toBe(35);. ....});. ..});. }); calculator.spec.ts

Slide 29

Slide 29 text

describe('Calculator',.().=>.{. ..beforeEach(...);. ..describe('#add',.().=>.{. ....it('sums.two.numbers',.().=>.{. ......expect(calc.add(2,.5)).toBe(7);. ....});. ....it('sums.three.numbers',.().=>.{. ......expect(calc.add(5,.10,.20)).toBe(35);. ....});. ..});. }); calculator.spec.ts

Slide 30

Slide 30 text

describe('Calculator',.().=>.{. ..beforeEach(...);. ..describe('#add',.().=>.{. ....it('sums.two.numbers',.().=>.{. ......expect(calc.add(2,.5)).toBe(7);. ....});. ....it('sums.three.numbers',.().=>.{. ......expect(calc.add(5,.10,.20)).toBe(35);. ....});. ..});. }); calculator.spec.ts Executed 2 of 2 (1 FAILED)

Slide 31

Slide 31 text

calculator.ts class.Calculator.{. ..add(...numbers).{. ....return.numbers.reduce((current,.total).=>.{. ......return.current.+.total;. ....},.0);. ..}. }

Slide 32

Slide 32 text

calculator.ts class.Calculator.{. ..add(...numbers).{. ....return.numbers.reduce((current,.total).=>.{. ......return.current.+.total;. ....},.0);. ..}. } Executed 2 of 2 SUCCESS

Slide 33

Slide 33 text

1. Keep tests small as possible (single purpose) 2. Test the public interfaces STRATEGIES

Slide 34

Slide 34 text

Whenever you can, write unit tests. They are cheap, fast and they can run often.

Slide 35

Slide 35 text

SETUP THE ENVIRONMENT

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

ANGULAR CLI https://github.com/angular/angular-cli

Slide 38

Slide 38 text

$.npm.install.Og.angularOcli

Slide 39

Slide 39 text

$.ng.new.testOproject

Slide 40

Slide 40 text

├──.angularOcliObuild.js. ├──.angularOcli.json. ├──.config. ├──.e2e. ├──.karmaOtestOshim.js. ├──.karma.conf.js. ├──.package.json. ├──.protractor.conf.js. ├──.public. ├──.src. ├──.tslint.json. ├──.typings. └──.typings.json

Slide 41

Slide 41 text

├──.angularOcliObuild.js. ├──.angularOcli.json. ├──.config. ├──.e2e. ├──.karmaOtestOshim.js. ├──.karma.conf.js. ├──.package.json. ├──.protractor.conf.js. ├──.public. ├──.src. ├──.tslint.json. ├──.typings. └──.typings.json └──.client. ....├──.app. ....│...├──.routeOconfig.ts. ....│...├──.testOproject.html. ....│...├──.testOproject.spec.ts. ....│...└──.testOproject.ts. ....├──.app.ts. ....├──.favicon.ico. ....├──.index.html. ....├──.tsconfig.json. ....└──.typings.d.ts

Slide 42

Slide 42 text

$.ng.test

Slide 43

Slide 43 text

$.ng.serve

Slide 44

Slide 44 text

TESTING ANGULAR

Slide 45

Slide 45 text

COMPONENTS

Slide 46

Slide 46 text

@Component({. ..selector:.'taOuser'. }). export.class.TaUser.{. ..@Input().user;. ..@Input().msg;. } ta-user.ts

Slide 47

Slide 47 text

. . {{.user.name.}}.is.{{.user.age.}}.and.his.favorite.color.is.. . {{.user.favoriteColor.}}. . {{.msg.}} ta-user.html

Slide 48

Slide 48 text

import.{. ..it,. ..describe,. ..expect,. ..injectAsync,. ..TestComponentBuilder. }.from.'angular2/testing';. import.{TaUser}.from.'./taOuser'; ta-user.spec.ts

Slide 49

Slide 49 text

import.{. ..it,. ..describe,. ..expect,. ..injectAsync,. ..TestComponentBuilder. }.from.'angular2/testing';. import.{TaUser}.from.'./taOuser'; ta-user.spec.ts

Slide 50

Slide 50 text

import.{. ..it,. ..describe,. ..expect,. ..injectAsync,. ..TestComponentBuilder. }.from.'angular2/testing';. import.{TaUser}.from.'./taOuser'; ta-user.spec.ts Inject providers in it blocks

Slide 51

Slide 51 text

import.{. ..it,. ..describe,. ..expect,. ..injectAsync,. ..TestComponentBuilder. }.from.'angular2/testing';. import.{TaUser}.from.'./taOuser'; ta-user.spec.ts

Slide 52

Slide 52 text

import.{. ..it,. ..describe,. ..expect,. ..injectAsync,. ..TestComponentBuilder. }.from.'angular2/testing';. import.{TaUser}.from.'./taOuser'; ta-user.spec.ts Instantiate components in it blocks

Slide 53

Slide 53 text

import.{. ..it,. ..describe,. ..expect,. ..injectAsync,. ..TestComponentBuilder. }.from.'angular2/testing';. import.{TaUser}.from.'./taOuser'; ta-user.spec.ts

Slide 54

Slide 54 text

import.{. ..it,. ..describe,. ..expect,. ..injectAsync,. ..TestComponentBuilder. }.from.'angular2/testing';. import.{TaUser}.from.'./taOuser'; ta-user.spec.ts

Slide 55

Slide 55 text

describe('TaUser.Component',.().=>.{. ..it('should.show.the.user.info',. ....injectAsync([TestComponentBuilder],(tcb:TestComponentBuilder).=>.{ ta-user.spec.ts

Slide 56

Slide 56 text

describe('TaUser.Component',.().=>.{. ..it('should.show.the.user.info',. ....injectAsync([TestComponentBuilder],(tcb:TestComponentBuilder).=>.{. ....return.tcb.createAsync(TaUser).then((fixture).=>.{. ......let.componentInstance.=.fixture.componentInstance;. ......let.element.=.fixture.nativeElement;. ......componentInstance.user.=.{.name:.'Ciro',.age:.18,.favoriteColor:.'black'.};. ......fixture.detectChanges();. ......expect(element.querySelector('h1').innerText). .........toBe('Ciro.is.18.and.his.favorite.color.is.black');. ....});. ..})); ta-user.spec.ts

Slide 57

Slide 57 text

ComponentFixture componentInstance nativeElement debugElement elementRef detectChanges() destroy()

Slide 58

Slide 58 text

SERVICES

Slide 59

Slide 59 text

@Injectable(). export.class.TaUserService.{. ..getUser(username:.string).{. ....return.new.Promise((resolve,.reject).=>.{. ......let.user.=.users.filter(u.=>.u.name.===.username);. ......if.(user.length).{. ........resolve(user[0]);. ......}.else.{. ........reject('Not.found.');. ......}. ....});. ..}. } ta-user-service.spec.ts

Slide 60

Slide 60 text

import.{. ..it,. ..describe,. ..expect,. ..beforeEachProviders,. ..inject,. ..fakeAsync,. ..tick. }.from.'angular2/testing';. import.{TaUserService}.from.'./taOuserOservice'; ta-user-service.spec.ts

Slide 61

Slide 61 text

import.{. ..it,. ..describe,. ..expect,. ..beforeEachProviders,. ..inject,. ..fakeAsync,. ..tick. }.from.'angular2/testing';. import.{TaUserService}.from.'./taOuserOservice'; ta-user-service.spec.ts Loads the providers using Angular's DI

Slide 62

Slide 62 text

import.{. ..it,. ..describe,. ..expect,. ..beforeEachProviders,. ..inject,. ..fakeAsync,. ..tick. }.from.'angular2/testing';. import.{TaUserService}.from.'./taOuserOservice'; ta-user-service.spec.ts

Slide 63

Slide 63 text

import.{. ..it,. ..describe,. ..expect,. ..beforeEachProviders,. ..inject,. ..fakeAsync,. ..tick. }.from.'angular2/testing';. import.{TaUserService}.from.'./taOuserOservice'; ta-user-service.spec.ts Inject the loaded providers in the it block

Slide 64

Slide 64 text

import.{. ..it,. ..describe,. ..expect,. ..beforeEachProviders,. ..inject,. ..fakeAsync,. ..tick. }.from.'angular2/testing';. import.{TaUserService}.from.'./taOuserOservice'; ta-user-service.spec.ts

Slide 65

Slide 65 text

import.{. ..it,. ..describe,. ..expect,. ..beforeEachProviders,. ..inject,. ..fakeAsync,. ..tick. }.from.'angular2/testing';. import.{TaUserService}.from.'./taOuserOservice'; ta-user-service.spec.ts Allows to test asynchronous in a sync fashion

Slide 66

Slide 66 text

import.{. ..it,. ..describe,. ..expect,. ..beforeEachProviders,. ..inject,. ..fakeAsync,. ..tick. }.from.'angular2/testing';. import.{TaUserService}.from.'./taOuserOservice'; ta-user-service.spec.ts

Slide 67

Slide 67 text

describe('TaUserService.Service',.().=>.{. ..beforeEachProviders(().=>.[TaUserService]); ta-user-service.spec.ts

Slide 68

Slide 68 text

describe('TaUserService.Service',.().=>.{. ..beforeEachProviders(().=>.[TaUserService]);. ..describe('#getUser',.().=>.{. ....it('should.get.the.user',. ......inject([TaUserService],.fakeAsync((service:.TaUserService).=>.{. ......let.user;. ......service.getUser('cironunes').then((value).=>.{. ........user.=.value;. ......});. ......tick();. ......expect(user.age).toBe(24);. ......expect(user.favoriteColor).toBe('black');. ....}))); ta-user-service.spec.ts

Slide 69

Slide 69 text

describe('TaUserService.Service',.().=>.{. ..beforeEachProviders(().=>.[TaUserService]);. ..describe('#getUser',.().=>.{. ....it('should.get.the.user',. ......inject([TaUserService],.fakeAsync((service:.TaUserService).=>.{. ......let.user;. ......service.getUser('cironunes').then((value).=>.{. ........user.=.value;. ......});. ......tick();. ......expect(user.age).toBe(24);. ......expect(user.favoriteColor).toBe('black');. ....}))); ta-user-service.spec.ts

Slide 70

Slide 70 text

DEMO https://github.com/cironunes/ng2-tests

Slide 71

Slide 71 text

SUMMARY

Slide 72

Slide 72 text

1. Unit tests helps us to test applications automatically SUMMARY

Slide 73

Slide 73 text

1. Unit tests helps us to test applications automatically 2. Good tests are small and test the public interface SUMMARY

Slide 74

Slide 74 text

1. Unit tests helps us to test applications automatically 2. Good tests are small and test the public interface 3. The CLI is the recommended way to get up and running SUMMARY

Slide 75

Slide 75 text

1. Unit tests helps us to test applications automatically 2. Good tests are small and test the public interface 3. The CLI is the recommended way to get up and running SUMMARY 4. The testing library wraps Jasmine and provide convenient features

Slide 76

Slide 76 text

1. Unit tests helps us to test applications automatically 2. Good tests are small and test the public interface 3. The CLI is the recommended way to get up and running SUMMARY 4. The testing library wraps Jasmine and provide convenient features 5. beforeEachProviders + inject/injectAsync to use the DI system

Slide 77

Slide 77 text

1. Unit tests helps us to test applications automatically 2. Good tests are small and test the public interface 3. The CLI is the recommended way to get up and running SUMMARY 4. The testing library wraps Jasmine and provide convenient features 5. beforeEachProviders + inject/injectAsync to use the DI system 6. provide to mock dependencies

Slide 78

Slide 78 text

SUMMARY

Slide 79

Slide 79 text

7. TestComponentBuilder returns the ComponentFixture instance SUMMARY

Slide 80

Slide 80 text

7. TestComponentBuilder returns the ComponentFixture instance 8. The ComponentFixture contains the component instance SUMMARY

Slide 81

Slide 81 text

7. TestComponentBuilder returns the ComponentFixture instance 8. The ComponentFixture contains the component instance 9. The ComponentFixture contains the DOM element SUMMARY

Slide 82

Slide 82 text

7. TestComponentBuilder returns the ComponentFixture instance 8. The ComponentFixture contains the component instance 9. The ComponentFixture contains the DOM element SUMMARY 10. fakeAsync and tick to test async code in a sync fashion

Slide 83

Slide 83 text

7. TestComponentBuilder returns the ComponentFixture instance 8. The ComponentFixture contains the component instance 9. The ComponentFixture contains the DOM element SUMMARY 10. fakeAsync and tick to test async code in a sync fashion 11. injectAsync will be deprecated in favor of async

Slide 84

Slide 84 text

1. Pipes 2. HTTP 3. Observables What wasn’t covered

Slide 85

Slide 85 text

THANK YOU! @cironunesdev http://cnun.es/a2-unit-testing