43
@kylehodgson www.kylehodgson.com

Testing AngularJS

Embed Size (px)

Citation preview

Page 1: Testing AngularJS

@kylehodgson www.kylehodgson.com

Page 2: Testing AngularJS
Page 3: Testing AngularJS

+

Page 4: Testing AngularJS

Testable Angular Writing testable code

Page 5: Testing AngularJS

Toy App: ToDontList Story Epic

As a busy executive with too much to do, I would like to see a list of things that are not worth my time,

So that I can avoid doing them.

Page 6: Testing AngularJS
Page 7: Testing AngularJS
Page 8: Testing AngularJS
Page 9: Testing AngularJS

How do you get at this code?

Testability

Page 10: Testing AngularJS

Namespace = {};

Testability

Page 11: Testing AngularJS
Page 12: Testing AngularJS
Page 13: Testing AngularJS

Unit tests should test your code

Testability

Page 14: Testing AngularJS

. ├── app │ ├── app.js │ ├── controllers │ │ └── list-controller.js │ └── services │ └── list-items-service.js ├── index.html └── items.json

Testability

Page 15: Testing AngularJS

app/services/list-items-service.js

app/services/list-items-service.js

Page 16: Testing AngularJS

app/app.js

app/app.js

Page 17: Testing AngularJS

specs/services/list-items_test.js

Page 18: Testing AngularJS

Testing Dependencies

Mocking

Page 19: Testing AngularJS
Page 20: Testing AngularJS

Open Source Libraries

•  RequireJS •  Sinon: mocking •  Karma: test runner •  Jasmine: unit test framework

o  Jasmine as promised

•  Chai: assertions o  Chai as promised

•  Duck Angular: interaction testing

Page 21: Testing AngularJS

Duck Angular Angular Integration Testing

Page 22: Testing AngularJS

Duck Angular

Run it in Karma Get it with Bower bower install duck-angular

Page 23: Testing AngularJS

Duck Angular & Karma

Page 24: Testing AngularJS

Duck Angular: Bindings

Page 25: Testing AngularJS

Duck Angular: Interactions

Page 26: Testing AngularJS
Page 27: Testing AngularJS
Page 28: Testing AngularJS

Anatomy of a Duck Angular spec

Duck Angular

Page 29: Testing AngularJS
Page 30: Testing AngularJS

•  Keep DOM manipulation in specific pieces; for instance, Directives, or in a service where required

DOM Manipulation

Page 31: Testing AngularJS

End to End AngularJS

Page 32: Testing AngularJS

E2E

http://docs.angularjs.org/guide/e2e-testing

sic: depricated is mis-spelled on angular’s site

Page 33: Testing AngularJS

Selenium

• Mature • Well understood •  “Enterprise strength” •  Terrible “SPA” support

Page 34: Testing AngularJS

Protractor Functional Testing for AngularJS

https://github.com/angular/protractor

Page 35: Testing AngularJS

Protractor

Not good (with) Karma.

Page 36: Testing AngularJS

searches for elements by matching binding names, either from ng-bind or {{}} notation in the template

by.binding

Page 37: Testing AngularJS

by.model

by.model searches for elements by input ng-model

Page 38: Testing AngularJS
Page 39: Testing AngularJS

by.repeater

by.repeater searches for ng-repeat elements.

Page 40: Testing AngularJS
Page 41: Testing AngularJS
Page 42: Testing AngularJS

Web server Selenium Java

Protractor pre-reqs

Page 43: Testing AngularJS

Kyle Hodgson www.kylehodgson.com @kylehodgson

Avishek “Mojo” Sen Gupta www.avishek.net @avisheksengupta

Questions? www.angularjs.org www.yearofmoo.com www.egghead.io www.pluralsight.com github.com/asengupta/duck-angular github.com/kylehodgson/angular-toy