Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Client Side Testing
Andrew Best
May 26, 2015
Programming
0
48
Client Side Testing
A quick trip around javascript and Angular unit testing using Jasmine and Chutzpah.
Andrew Best
May 26, 2015
Tweet
Share
More Decks by Andrew Best
See All by Andrew Best
Learn Authentication The Hard Way
andrewabest
0
120
Finding The Sweet Spot BNE
andrewabest
1
160
Finding The Sweet Spot
andrewabest
0
240
Automating AWS
andrewabest
2
150
Conventional Wisdom
andrewabest
1
200
DSC a-b-c's
andrewabest
0
89
AWS a-b-c's
andrewabest
3
140
What is Git?
andrewabest
0
190
Seven Year Itch
andrewabest
0
150
Other Decks in Programming
See All in Programming
Reactは何を提供するLibraryなのか?
taro28
3
420
A Philosophy of Software Design 後半
yosuke_furukawa
PRO
10
2.8k
Get Ready for Jakarta EE 10
ivargrimstad
0
2.5k
模組化的Swift架構(二) DDD速成
haifengkao
0
390
IE Graduation Certificate
jxck
6
4.8k
io22 extended What's new in app performance
veronikapj
0
340
こそこそアジャイル導入しようぜ!
ichimichi
0
1.2k
Modern Android Developer ~ 안내서
pluu
1
640
クックパッドマートの失敗したデータ設計 Before / After 大放出
mokuzon
0
170
Android Compose Component - mapping.
taehwandev
0
140
What's new in Android development tools まとめ
mkeeda
0
350
Improving Developer Experience Through Tools and Techniques 2022
krzysztofzablocki
0
1.1k
Featured
See All Featured
Designing with Data
zakiwarfel
91
3.9k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
10
3.4k
jQuery: Nuts, Bolts and Bling
dougneiner
56
6.4k
The World Runs on Bad Software
bkeepers
PRO
57
5.3k
How to Ace a Technical Interview
jacobian
265
21k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
39
13k
From Idea to $5000 a Month in 5 Months
shpigford
373
44k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
105
16k
GraphQLの誤解/rethinking-graphql
sonatard
28
6.6k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
315
19k
Design by the Numbers
sachag
271
17k
The Cult of Friendly URLs
andyhume
68
4.8k
Transcript
Testing the web with Chutzpah and Jasmine
Page “I’m a c# guy that uses NUnit” › In
the javascriptworld, Jasmine is your NUnitequivalent. (http://jasmine.github.io/) › In Jasmine, you create ‘Suites’ that describe a set of expectations you have for the thing you want to test. › Jasmine also allows you to also set your expectations within these suites. / Copyright ©2014 by Readify Pty Ltd 2
Page So how do I do X in Jasmine? ›
Setup and teardown: › Ignore tests: / Copyright ©2014 by Readify Pty Ltd 3
Page Cool, now how do I run my tests? ›
Chutzpah! (https://github.com/mmanela/chutzpah) › Chutzpah generates html to run our specs with Jasmine in a headless browser (phantomjs). › It will also generate html to run our specs in our browser of choice (handy for debugging tests!) › It also takes care of loading dependencies for us where needed. / Copyright ©2014 by Readify Pty Ltd 4
Page So how to we hook it all up? ›
Install-Package Chutzpah › Install the Chutzpah VS Extensions › Add and configure your chutzpah.json › Don’t worry about installing Jasmine! Chutzpah bundles it up for you › Write your hello world unit test! / Copyright ©2014 by Readify Pty Ltd 5
Page Man this thing is busted! › Chutzpah is a
bit of a black box when used from VS – if it doesn’t seem to be working from VS, you can always use the command line runner. › Running chutzpah.console.exe {fullpathtofile.js} /trace will give you a detailed log of the test run named chutzpah.log alongside the console exe (in your packages folder) / Copyright ©2014 by Readify Pty Ltd 6
Page Okay cool, now how about Ng? › To test
an Ng app, because of how angular’smodule / DI system works, we need to make sure we include all necessary references to the applications scripts in our chutzpah.json › If you have a bunch of bootstrapping activity in your standard app.js, you may want to craft a slimmed down version for testing / Copyright ©2014 by Readify Pty Ltd 7
Page Dude, where’s my Mocks? › ngMock(https://docs.angularjs.org/api/ngMock) › Injects and
mocks most of the core angular services for us, such as $http and $log › It *must* be loaded by Chutzpah after angular.js (it overrides default behaviour) › No further wireuprequired! › Jasmine also provides some mocking functions similar to NSubstitute / Copyright ©2014 by Readify Pty Ltd 8
Page Example Ng Test / Copyright ©2014 by Readify Pty
Ltd 9 describe("Landing Page Directive", function() { var $sut; describe('If form is started', function() { var goToDashboardCalled = false; beforeEach(function() { module(function($provide) { $provide.service('utilityService', function() { return { goToDashboard: function() { goToDashboardCalled = true; } } }); }); inject(function($compile, $rootScope, $httpBackend) { $httpBackend.expectPOST('apiBaseUrlforms/getbasicformdetails').respond(200); $rootScope.formStarted = true; var elem = angular.element("<div landing-page></div>"); $sut = $compile(elem)($rootScope); $rootScope.$digest(); }); }); it('Redirects to the dashboard.', function() { expect(goToDashboardCalled).toBe(true); }); }); });
Page Hey what were those things? › Module: allows us
to replace services to be injected into our class under test. This allows us to mockother things out. › Inject: allows us to mutate mocked dependencies to set them up for our SUT, and also supply them when building our SUT. › These are both given to us via ngMock, and for ease of use are attached to the window object for us. / Copyright ©2014 by Readify Pty Ltd 10
Page Ng testing gotchas › Http interactions *must* be mocked.
› You can only set up dependencies via Module() before your call to Inject(). If you call it after… BOOM. › If something doesn’t seem to be working, remember to double check your chutzpah.jsonand the html generated by Chutzpah for script errors! / Copyright ©2014 by Readify Pty Ltd 11
Page Staying DRY › So you have a bunch of
code you want to run before every test? Sick of copy pasta? › Jasmine allows us to define a spec-helper.js file which can include global beforeEvery and afterEvery functions! › This can be a useful place to bootstrap things like Ng constants, if you have some. / Copyright ©2014 by Readify Pty Ltd 12
Page Bonus slide: CI › So now I have my
tests, how do I plug them into my CI pipeline? › Use the Chutzpah console exe that you checked in with your chutzpah package because you are a good developer and detest package restore. › And it goes a little something like this… / Copyright ©2014 by Readify Pty Ltd 13
None