Share
Embed
Unit Testing in Angular
Demo: https://github.com/cironunes/ng2-tests
1. http://cnun.es/a2-testing-recipes (@gerardsans)
2. https://www.youtube.com/watch?v=C0F2E-PRm44
3. http://github.com/juliemr/ng2-test-seed
4. http://betterspecs.org/
Ciro Nunes
April 13, 2016
Transcript
85 slides
-
1. Unit testing 101 2. Setup Angular testing environment 3.
Test Angular services and components AGENDA -
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) -
1. Keep tests small as possible (single purpose) 2. Test
the public interfaces STRATEGIES -
Whenever you can, write unit tests. They are cheap, fast
and they can run often. -
├──.angularOcliObuild.js. ├──.angularOcli.json. ├──.config. ├──.e2e. ├──.karmaOtestOshim.js. ├──.karma.conf.js. ├──.package.json. ├──.protractor.conf.js. ├──.public. ├──.src.
├──.tslint.json. ├──.typings. └──.typings.json -
├──.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 -
@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 -
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 -
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 -
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 -
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 -
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 -
1. Unit tests helps us to test applications automatically 2.
Good tests are small and test the public interface SUMMARY -
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 -
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 -
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 -
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 -
7. TestComponentBuilder returns the ComponentFixture instance 8. The ComponentFixture contains
the component instance SUMMARY -
7. TestComponentBuilder returns the ComponentFixture instance 8. The ComponentFixture contains
the component instance 9. The ComponentFixture contains the DOM element SUMMARY -
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 -
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